home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / app / gimpimage.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-05-08  |  100.2 KB  |  3,940 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 2 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program; if not, write to the Free Software
  16.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include <string.h>
  22.  
  23. #include <gtk/gtk.h>
  24.  
  25. #include "apptypes.h"
  26.  
  27. #include "cursorutil.h"
  28. #include "drawable.h"
  29. #include "floating_sel.h"
  30. #include "gdisplay.h"
  31. #include "gimage_mask.h"
  32. #include "gimpcontext.h"
  33. #include "paint_funcs.h"
  34. #include "parasitelist.h"
  35. #include "undo.h"
  36. #include "gimpsignal.h"
  37. #include "gimpparasite.h"
  38. #include "path.h"
  39. #include "gimprc.h"
  40.  
  41. #include "tile_manager.h"
  42. #include "tile.h"
  43. #include "layer_pvt.h"
  44. #include "drawable_pvt.h"        /* ick ick. */
  45.  
  46. #include "libgimp/gimpcolorspace.h"
  47. #include "libgimp/gimplimits.h"
  48. #include "libgimp/gimpmath.h"
  49. #include "libgimp/gimpparasite.h"
  50.  
  51. #include "libgimp/gimpintl.h"
  52.  
  53.  
  54. #ifdef DEBUG
  55. #define TRC(x) printf x
  56. #else
  57. #define TRC(x)
  58. #endif
  59.  
  60.  
  61. /*  Local function declarations  */
  62. static void     gimp_image_destroy               (GtkObject *object);
  63. static void     gimp_image_free_projection       (GimpImage *);
  64. static void     gimp_image_allocate_shadow       (GimpImage *, gint, gint, gint);
  65. static void     gimp_image_allocate_projection   (GimpImage *);
  66. static void     gimp_image_free_layers           (GimpImage *);
  67. static void     gimp_image_free_channels         (GimpImage *);
  68. static void     gimp_image_construct_layers      (GimpImage *, gint, gint, gint, gint);
  69. static void     gimp_image_construct_channels    (GimpImage *, gint, gint, gint, gint);
  70. static void     gimp_image_initialize_projection (GimpImage *, gint, gint, gint, gint);
  71. static void     gimp_image_get_active_channels   (GimpImage *, GimpDrawable *, gint *);
  72.  
  73. /*  projection functions  */
  74. static void     project_intensity            (GimpImage *, Layer *,
  75.                           PixelRegion *, PixelRegion *,
  76.                           PixelRegion *);
  77. static void     project_intensity_alpha      (GimpImage *, Layer *,
  78.                           PixelRegion *, PixelRegion *,
  79.                           PixelRegion *);
  80. static void     project_indexed              (GimpImage *, Layer *,
  81.                           PixelRegion *, PixelRegion *);
  82. static void     project_channel              (GimpImage *, Channel *,
  83.                           PixelRegion *, PixelRegion *);
  84.  
  85. /*
  86.  *  Global variables
  87.  */
  88. gint valid_combinations[][MAX_CHANNELS + 1] =
  89. {
  90.   /* RGB GIMAGE */
  91.   { -1, -1, -1, COMBINE_INTEN_INTEN, COMBINE_INTEN_INTEN_A },
  92.   /* RGBA GIMAGE */
  93.   { -1, -1, -1, COMBINE_INTEN_A_INTEN, COMBINE_INTEN_A_INTEN_A },
  94.   /* GRAY GIMAGE */
  95.   { -1, COMBINE_INTEN_INTEN, COMBINE_INTEN_INTEN_A, -1, -1 },
  96.   /* GRAYA GIMAGE */
  97.   { -1, COMBINE_INTEN_A_INTEN, COMBINE_INTEN_A_INTEN_A, -1, -1 },
  98.   /* INDEXED GIMAGE */
  99.   { -1, COMBINE_INDEXED_INDEXED, COMBINE_INDEXED_INDEXED_A, -1, -1 },
  100.   /* INDEXEDA GIMAGE */
  101.   { -1, -1, COMBINE_INDEXED_A_INDEXED_A, -1, -1 },
  102. };
  103.  
  104. guint32 next_guide_id = 1;  /* For generating guide_ID handles for PDB stuff */
  105.  
  106.  
  107. /*
  108.  *  Static variables
  109.  */
  110.  
  111. enum {
  112.   CLEAN,
  113.   DIRTY,
  114.   REPAINT,
  115.   RENAME,
  116.   RESIZE,
  117.   RESTRUCTURE,
  118.   COLORMAP_CHANGED,
  119.   UNDO_EVENT,
  120.   LAYER_MERGE,
  121.   LAST_SIGNAL
  122. };
  123.  
  124. static guint            gimp_image_signals[LAST_SIGNAL];
  125. static GimpObjectClass *parent_class;
  126.  
  127.  
  128. static void
  129. gimp_image_class_init (GimpImageClass *klass)
  130. {
  131.   GtkObjectClass *object_class;
  132.   GtkType         type;
  133.   
  134.   object_class = GTK_OBJECT_CLASS(klass);
  135.   parent_class = gtk_type_class (gimp_object_get_type ());
  136.   
  137.   type=object_class->type;
  138.  
  139.   object_class->destroy =  gimp_image_destroy;
  140.  
  141.   gimp_image_signals[CLEAN] =
  142.       gimp_signal_new ("clean", GTK_RUN_FIRST, type, 0,
  143.                gimp_sigtype_void);
  144.   gimp_image_signals[DIRTY] =
  145.       gimp_signal_new ("dirty", GTK_RUN_FIRST, type, 0,
  146.                gimp_sigtype_void);
  147.   gimp_image_signals[REPAINT] =
  148.       gimp_signal_new ("repaint", GTK_RUN_FIRST, type, 0,
  149.                gimp_sigtype_int_int_int_int);
  150.   gimp_image_signals[RENAME] =
  151.       gimp_signal_new ("rename", GTK_RUN_FIRST, type, 0,
  152.                gimp_sigtype_void);
  153.   gimp_image_signals[RESIZE] =
  154.       gimp_signal_new ("resize", GTK_RUN_FIRST, type, 0,
  155.                gimp_sigtype_void);
  156.   gimp_image_signals[RESTRUCTURE] =
  157.       gimp_signal_new ("restructure", GTK_RUN_FIRST, type, 0,
  158.                gimp_sigtype_void);
  159.   gimp_image_signals[COLORMAP_CHANGED] =
  160.       gimp_signal_new ("colormap_changed", GTK_RUN_FIRST, type, 0,
  161.                gimp_sigtype_int);
  162.   gimp_image_signals[UNDO_EVENT] = 
  163.       gimp_signal_new ("undo_event", GTK_RUN_FIRST, type, 0,
  164.                gimp_sigtype_int);
  165.   gimp_image_signals[LAYER_MERGE] =
  166.       gimp_signal_new ("layer_merge", GTK_RUN_FIRST, type, 0,
  167.                gimp_sigtype_void);
  168.   
  169.   gtk_object_class_add_signals (object_class, gimp_image_signals, LAST_SIGNAL);
  170. }
  171.  
  172.  
  173. /* static functions */
  174.  
  175. static void 
  176. gimp_image_init (GimpImage *gimage)
  177. {
  178.   gimage->has_filename          = FALSE;
  179.   gimage->num_cols              = 0;
  180.   gimage->cmap                  = NULL;
  181.   gimage->disp_count            = 0;
  182.   gimage->instance_count        = 0;
  183.   gimage->shadow                = NULL;
  184.   gimage->dirty                 = 1;
  185.   gimage->undo_on               = TRUE;
  186.   gimage->construct_flag        = -1;
  187.   gimage->tattoo_state          = 0;
  188.   gimage->projection            = NULL;
  189.   gimage->guides                = NULL;
  190.   gimage->layers                = NULL;
  191.   gimage->channels              = NULL;
  192.   gimage->layer_stack           = NULL;
  193.   gimage->undo_stack            = NULL;
  194.   gimage->redo_stack            = NULL;
  195.   gimage->undo_bytes            = 0;
  196.   gimage->undo_levels           = 0;
  197.   gimage->group_count           = 0;
  198.   gimage->pushing_undo_group    = UNDO_NULL;
  199.   gimage->comp_preview_valid[0] = FALSE;
  200.   gimage->comp_preview_valid[1] = FALSE;
  201.   gimage->comp_preview_valid[2] = FALSE;
  202.   gimage->comp_preview          = NULL;
  203.   gimage->parasites             = parasite_list_new ();
  204.   gimage->xresolution           = default_xresolution;
  205.   gimage->yresolution           = default_yresolution;
  206.   gimage->unit                  = default_units;
  207.   gimage->save_proc             = NULL;
  208.   gimage->paths                 = NULL;
  209. }
  210.  
  211. GtkType 
  212. gimp_image_get_type (void) 
  213. {
  214.   static GtkType type;
  215.  
  216.   GIMP_TYPE_INIT (type,
  217.           GimpImage,
  218.           GimpImageClass,
  219.           gimp_image_init,
  220.           gimp_image_class_init,
  221.           GIMP_TYPE_OBJECT);
  222.   return type;
  223. }
  224.  
  225. static void
  226. gimp_image_allocate_projection (GimpImage *gimage)
  227. {
  228.   if (gimage->projection)
  229.     gimp_image_free_projection (gimage);
  230.  
  231.   /*  Find the number of bytes required for the projection.
  232.    *  This includes the intensity channels and an alpha channel
  233.    *  if one doesn't exist.
  234.    */
  235.   switch (gimp_image_base_type (gimage))
  236.     {
  237.     case RGB:
  238.     case INDEXED:
  239.       gimage->proj_bytes = 4;
  240.       gimage->proj_type = RGBA_GIMAGE;
  241.       break;
  242.     case GRAY:
  243.       gimage->proj_bytes = 2;
  244.       gimage->proj_type = GRAYA_GIMAGE;
  245.       break;
  246.     default:
  247.       g_assert_not_reached ();
  248.     }
  249.  
  250.   /*  allocate the new projection  */
  251.   gimage->projection = tile_manager_new (gimage->width, gimage->height, gimage->proj_bytes);
  252.   tile_manager_set_user_data (gimage->projection, (void *) gimage);
  253.   tile_manager_set_validate_proc (gimage->projection, gimp_image_validate);
  254. }
  255.  
  256. static void
  257. gimp_image_free_projection (GimpImage *gimage)
  258. {
  259.   if (gimage->projection)
  260.     tile_manager_destroy (gimage->projection);
  261.  
  262.   gimage->projection = NULL;
  263. }
  264.  
  265. static void
  266. gimp_image_allocate_shadow (GimpImage *gimage, 
  267.                 gint       width, 
  268.                 gint       height, 
  269.                 gint       bpp)
  270. {
  271.   /*  allocate the new projection  */
  272.   gimage->shadow = tile_manager_new (width, height, bpp);
  273. }
  274.  
  275.  
  276. /* function definitions */
  277.  
  278. GimpImage *
  279. gimp_image_new (gint               width,
  280.         gint               height,
  281.         GimpImageBaseType  base_type)
  282. {
  283.   GimpImage *gimage = GIMP_IMAGE (gtk_type_new (gimp_image_get_type ()));
  284.   gint i;
  285.  
  286.   gimage->filename  = NULL;
  287.   gimage->width     = width;
  288.   gimage->height    = height;
  289.   gimage->base_type = base_type;
  290.  
  291.   switch (base_type)
  292.     {
  293.     case RGB:
  294.     case GRAY:
  295.       break;
  296.     case INDEXED:
  297.       /* always allocate 256 colors for the colormap */
  298.       gimage->num_cols = 0;
  299.       gimage->cmap     = (guchar *) g_malloc0 (COLORMAP_SIZE);
  300.       break;
  301.     default:
  302.       break;
  303.     }
  304.  
  305.   /*  configure the active pointers  */
  306.   gimage->active_layer   = NULL;
  307.   gimage->active_channel = NULL;  /* no default active channel */
  308.   gimage->floating_sel   = NULL;
  309.  
  310.   /*  set all color channels visible and active  */
  311.   for (i = 0; i < MAX_CHANNELS; i++)
  312.     {
  313.       gimage->visible[i] = TRUE;
  314.       gimage->active[i]  = TRUE;
  315.     }
  316.  
  317.   /* create the selection mask */
  318.   gimage->selection_mask = channel_new_mask (gimage,
  319.                          gimage->width, gimage->height);
  320.  
  321.   /* set the qmask properties */
  322.   gimage->qmask_state    = FALSE;
  323.   gimage->qmask_opacity  = 50;
  324.   gimage->qmask_color[0] = 255; 
  325.   gimage->qmask_color[1] = 0; 
  326.   gimage->qmask_color[2] = 0; 
  327.  
  328.   return gimage;
  329. }
  330.  
  331. void
  332. gimp_image_set_filename (GimpImage *gimage, 
  333.              gchar     *filename)
  334. {
  335.   gchar    *new_filename;
  336.   gchar    *old_filename;
  337.   gboolean  free_old;
  338.  
  339.   /* 
  340.    * WARNING: this function will free the current filename even if you are 
  341.    * setting it to itself so any pointer you hold to the filename will be
  342.    * invalid after this call.  So please use with care.
  343.    */
  344.  
  345.   new_filename = g_strdup (filename);
  346.   old_filename = gimage->filename;
  347.   free_old = gimage->has_filename;
  348.  
  349.   if (filename && filename[0])
  350.     {
  351.       gimage->filename = new_filename;
  352.       gimage->has_filename = TRUE;
  353.     }
  354.   else
  355.     {
  356.       gimage->filename = NULL;
  357.       gimage->has_filename = FALSE;
  358.     }
  359.  
  360.   if (free_old)
  361.     g_free (old_filename);
  362.  
  363.   gtk_signal_emit (GTK_OBJECT (gimage), gimp_image_signals[RENAME]);
  364. }
  365.  
  366. void
  367. gimp_image_set_resolution (GimpImage *gimage,
  368.                gdouble    xresolution,
  369.                gdouble    yresolution)
  370. {
  371.   /* nothing to do if setting res to the same as before */
  372.   if ((ABS (gimage->xresolution - xresolution) < 1e-5) &&
  373.       (ABS (gimage->yresolution - yresolution) < 1e-5))
  374.       return;
  375.  
  376.   /* don't allow to set the resolution out of bounds */
  377.   if (xresolution < GIMP_MIN_RESOLUTION || xresolution > GIMP_MAX_RESOLUTION ||
  378.       yresolution < GIMP_MIN_RESOLUTION || yresolution > GIMP_MAX_RESOLUTION)
  379.     return;
  380.  
  381.   undo_push_resolution (gimage);
  382.  
  383.   gimage->xresolution = xresolution;
  384.   gimage->yresolution = yresolution;
  385.  
  386.   /* really just want to recalc size and repaint */
  387.   gdisplays_shrink_wrap (gimage);
  388. }
  389.  
  390. void
  391. gimp_image_get_resolution (const GimpImage *gimage,
  392.                gdouble   *xresolution,
  393.                gdouble   *yresolution)
  394. {
  395.   g_return_if_fail (xresolution && yresolution);
  396.  
  397.   *xresolution = gimage->xresolution;
  398.   *yresolution = gimage->yresolution;
  399. }
  400.  
  401. void
  402. gimp_image_set_unit (GimpImage *gimage,
  403.              GimpUnit   unit)
  404. {
  405.   undo_push_resolution (gimage);
  406.  
  407.   gimage->unit = unit;
  408. }
  409.  
  410. GimpUnit
  411. gimp_image_get_unit (const GimpImage *gimage)
  412. {
  413.   return gimage->unit;
  414. }
  415.  
  416. void
  417. gimp_image_set_save_proc (GimpImage     *gimage, 
  418.               PlugInProcDef *proc)
  419. {
  420.   gimage->save_proc = proc;
  421. }
  422.  
  423. PlugInProcDef *
  424. gimp_image_get_save_proc (const GimpImage *gimage)
  425. {
  426.   return gimage->save_proc;
  427. }
  428.  
  429. void
  430. gimp_image_resize (GimpImage *gimage, 
  431.            gint       new_width, 
  432.            gint       new_height,
  433.            gint       offset_x, 
  434.            gint       offset_y)
  435. {
  436.   Channel *channel;
  437.   Layer   *layer;
  438.   Layer   *floating_layer;
  439.   GSList  *list;
  440.   GList   *guide_list;
  441.  
  442.   gimp_add_busy_cursors();
  443.  
  444.   g_assert (new_width > 0 && new_height > 0);
  445.  
  446.   /*  Get the floating layer if one exists  */
  447.   floating_layer = gimp_image_floating_sel (gimage);
  448.  
  449.   undo_push_group_start (gimage, IMAGE_RESIZE_UNDO);
  450.  
  451.   /*  Relax the floating selection  */
  452.   if (floating_layer)
  453.     floating_sel_relax (floating_layer, TRUE);
  454.  
  455.   /*  Push the image size to the stack  */
  456.   undo_push_gimage_mod (gimage);
  457.  
  458.   /*  Set the new width and height  */
  459.   gimage->width  = new_width;
  460.   gimage->height = new_height;
  461.  
  462.   /*  Resize all channels  */
  463.   list = gimage->channels;
  464.   while (list)
  465.     {
  466.       channel = (Channel *) list->data;
  467.       channel_resize (channel, new_width, new_height, offset_x, offset_y);
  468.       list = g_slist_next (list);
  469.  
  470.     }
  471.  
  472.   /*  Reposition or remove any guides  */
  473.   guide_list = gimage->guides;
  474.   while (guide_list)
  475.     {
  476.       Guide *guide;
  477.  
  478.       guide = (Guide*) guide_list->data;
  479.       guide_list = g_list_next (guide_list);
  480.  
  481.       switch (guide->orientation)
  482.     {
  483.     case ORIENTATION_HORIZONTAL:
  484.       undo_push_guide (gimage, guide);
  485.       guide->position += offset_y;
  486.       if (guide->position < 0 || guide->position > new_height)
  487.         gimp_image_delete_guide (gimage, guide);
  488.       break;
  489.     case ORIENTATION_VERTICAL:
  490.       undo_push_guide (gimage, guide);
  491.       guide->position += offset_x;
  492.       if (guide->position < 0 || guide->position > new_width)
  493.         gimp_image_delete_guide (gimage, guide);
  494.       break;
  495.     default:
  496.       g_error("Unknown guide orientation I.\n");
  497.     }
  498.     }
  499.  
  500.   /*  Don't forget the selection mask!  */
  501.   channel_resize (gimage->selection_mask,
  502.           new_width, new_height, offset_x, offset_y);
  503.   gimage_mask_invalidate (gimage);
  504.  
  505.   /*  Reposition all layers  */
  506.   list = gimage->layers;
  507.   while (list)
  508.     {
  509.       layer = (Layer *) list->data;
  510.       layer_translate (layer, offset_x, offset_y);
  511.       list = g_slist_next (list);
  512.     }
  513.  
  514.   /*  Make sure the projection matches the gimage size  */
  515.   gimp_image_projection_realloc (gimage);
  516.  
  517.   /*  Rigor the floating selection  */
  518.   if (floating_layer)
  519.     floating_sel_rigor (floating_layer, TRUE);
  520.  
  521.   gtk_signal_emit (GTK_OBJECT (gimage), gimp_image_signals[RESIZE]);
  522.  
  523.   gimp_remove_busy_cursors (NULL);
  524. }
  525.  
  526. gint
  527. gimp_image_get_width (const GimpImage *gimage)
  528. {
  529.   return gimage->width;
  530. }
  531.  
  532. gint
  533. gimp_image_get_height (const GimpImage *gimage)
  534. {
  535.   return gimage->height;
  536. }
  537.  
  538. void
  539. gimp_image_scale (GimpImage *gimage, 
  540.           gint       new_width, 
  541.           gint       new_height)
  542. {
  543.   Channel *channel;
  544.   Layer   *layer;
  545.   Layer   *floating_layer;
  546.   GSList  *list;
  547.   GSList  *remove = NULL;
  548.   GList   *glist;
  549.   Guide   *guide;
  550.   gint     old_width;
  551.   gint     old_height;
  552.   gdouble  img_scale_w = 1.0;
  553.   gdouble  img_scale_h = 1.0;
  554.  
  555.   if ((new_width == 0) || (new_height == 0))
  556.     {
  557.       g_message (("gimp_image_scale: Scaling to zero width or height has been rejected."));
  558.       return;
  559.     }
  560.  
  561.   gimp_add_busy_cursors ();
  562.  
  563.   /*  Get the floating layer if one exists  */
  564.   floating_layer = gimp_image_floating_sel (gimage);
  565.  
  566.   undo_push_group_start (gimage, IMAGE_SCALE_UNDO);
  567.  
  568.   /*  Relax the floating selection  */
  569.   if (floating_layer)
  570.     floating_sel_relax (floating_layer, TRUE);
  571.  
  572.   /*  Push the image size to the stack  */
  573.   undo_push_gimage_mod (gimage);
  574.  
  575.   /*  Set the new width and height  */
  576.  
  577.   old_width      = gimage->width;
  578.   old_height     = gimage->height;
  579.   gimage->width  = new_width;
  580.   gimage->height = new_height;
  581.   img_scale_w    = (gdouble)new_width / (gdouble)old_width;
  582.   img_scale_h    = (gdouble)new_height / (gdouble)old_height;
  583.  
  584.   /*  Scale all channels  */
  585.   for (list = gimage->channels; list; list = g_slist_next (list))
  586.     {
  587.       channel = (Channel *) list->data;
  588.       channel_scale (channel, new_width, new_height);
  589.     }
  590.  
  591.   /*  Don't forget the selection mask!  */
  592.   /*  if (channel_is_empty(gimage->selection_mask))
  593.         channel_resize(gimage->selection_mask, new_width, new_height, 0, 0)
  594.       else
  595.   */
  596.         
  597.   channel_scale (gimage->selection_mask, new_width, new_height);
  598.   gimage_mask_invalidate (gimage);
  599.  
  600.   /*  Scale all layers  */
  601.   list = gimage->layers;
  602.   for (list = gimage->layers; list; list = g_slist_next (list))
  603.     {
  604.       layer = (Layer *) list->data;
  605.       if (layer_scale_by_factors (layer, img_scale_w, img_scale_h) == FALSE)
  606.     {
  607.       /* Since 0 < img_scale_w, img_scale_h, failure due to one or more     */
  608.       /* vanishing scaled layer dimensions. Implicit delete implemented     */
  609.       /* here. Upstream warning implemented in resize_check_layer_scaling() */
  610.           /* [resize.c line 1295], which offers the user the chance to bail out.*/
  611.  
  612.           remove = g_slist_append (remove, layer);
  613.         }
  614.     }
  615.   /* We defer removing layers lost to scaling until now            */
  616.   /* so as not to mix the operations of iterating over and removal */
  617.   /* from gimage->layers.                                          */  
  618.  
  619.   for (list = remove; list; list = g_slist_next (list))
  620.     {
  621.       layer = list->data;
  622.       gimage_remove_layer (gimage, layer);
  623.     }
  624.   g_slist_free (remove);
  625.  
  626.   /*  Scale any Guides  */
  627.   for (glist = gimage->guides; glist; glist = g_list_next (glist))
  628.     {
  629.       guide = (Guide*) glist->data;
  630.  
  631.       switch (guide->orientation)
  632.     {
  633.     case ORIENTATION_HORIZONTAL:
  634.       undo_push_guide (gimage, guide);
  635.       guide->position = (guide->position * new_height) / old_height;
  636.       break;
  637.     case ORIENTATION_VERTICAL:
  638.       undo_push_guide (gimage, guide);
  639.       guide->position = (guide->position * new_width) / old_width;
  640.       break;
  641.     default:
  642.       g_error("Unknown guide orientation II.\n");
  643.     }
  644.     }
  645.  
  646.   /*  Make sure the projection matches the gimage size  */
  647.   gimp_image_projection_realloc (gimage);
  648.  
  649.   /*  Rigor the floating selection  */
  650.   if (floating_layer)
  651.     floating_sel_rigor (floating_layer, TRUE);
  652.  
  653.   gtk_signal_emit (GTK_OBJECT (gimage), gimp_image_signals[RESIZE]);
  654.  
  655.   gimp_remove_busy_cursors (NULL);
  656. }
  657.  
  658. TileManager *
  659. gimp_image_shadow (GimpImage *gimage, 
  660.            gint       width, 
  661.            gint       height, 
  662.            gint       bpp)
  663. {
  664.   if (gimage->shadow &&
  665.       ((width != tile_manager_level_width (gimage->shadow)) ||
  666.        (height != tile_manager_level_height (gimage->shadow)) ||
  667.        (bpp != tile_manager_level_bpp (gimage->shadow))))
  668.     gimp_image_free_shadow (gimage);
  669.   else if (gimage->shadow)
  670.     return gimage->shadow;
  671.  
  672.   gimp_image_allocate_shadow (gimage, width, height, bpp);
  673.  
  674.   return gimage->shadow;
  675. }
  676.  
  677. void
  678. gimp_image_free_shadow (GimpImage *gimage)
  679. {
  680.   /*  Free the shadow buffer from the specified gimage if it exists  */
  681.   if (gimage->shadow)
  682.     tile_manager_destroy (gimage->shadow);
  683.  
  684.   gimage->shadow = NULL;
  685. }
  686.  
  687. static void
  688. gimp_image_destroy (GtkObject *object)
  689. {
  690.   GimpImage* gimage = GIMP_IMAGE (object);
  691.  
  692.   gimp_image_free_projection (gimage);
  693.   gimp_image_free_shadow (gimage);
  694.   
  695.   if (gimage->cmap)
  696.     g_free (gimage->cmap);
  697.   
  698.   if (gimage->has_filename)
  699.     g_free (gimage->filename);
  700.   
  701.   gimp_image_free_layers (gimage);
  702.   gimp_image_free_channels (gimage);
  703.   channel_delete (gimage->selection_mask);
  704.  
  705.   if (gimage->comp_preview)
  706.     temp_buf_free (gimage->comp_preview);
  707.  
  708.   if (gimage->parasites)
  709.     gtk_object_unref (GTK_OBJECT (gimage->parasites));
  710. }
  711.  
  712. void
  713. gimp_image_apply_image (GimpImage     *gimage,
  714.             GimpDrawable     *drawable,
  715.             PixelRegion     *src2PR,
  716.             gint              undo,
  717.             gint              opacity,
  718.             LayerModeEffects  mode,
  719.             /*  alternative to using drawable tiles as src1: */
  720.             TileManager     *src1_tiles,
  721.             gint              x,
  722.             gint              y)
  723. {
  724.   Channel     *mask;
  725.   gint         x1, y1, x2, y2;
  726.   gint         offset_x, offset_y;
  727.   PixelRegion  src1PR, destPR, maskPR;
  728.   gint         operation;
  729.   gint         active [MAX_CHANNELS];
  730.  
  731.   /*  get the selection mask if one exists  */
  732.   mask = (gimage_mask_is_empty (gimage)) ? NULL : gimp_image_get_mask (gimage);
  733.  
  734.   /*  configure the active channel array  */
  735.   gimp_image_get_active_channels (gimage, drawable, active);
  736.  
  737.   /*  determine what sort of operation is being attempted and
  738.    *  if it's actually legal...
  739.    */
  740.   operation = valid_combinations [drawable_type (drawable)][src2PR->bytes];
  741.   if (operation == -1)
  742.     {
  743.       g_message ("gimp_image_apply_image sent illegal parameters");
  744.       return;
  745.     }
  746.  
  747.   /*  get the layer offsets  */
  748.   drawable_offsets (drawable, &offset_x, &offset_y);
  749.  
  750.   /*  make sure the image application coordinates are within gimage bounds  */
  751.   x1 = CLAMP (x, 0, drawable_width  (drawable));
  752.   y1 = CLAMP (y, 0, drawable_height (drawable));
  753.   x2 = CLAMP (x + src2PR->w, 0, drawable_width  (drawable));
  754.   y2 = CLAMP (y + src2PR->h, 0, drawable_height (drawable));
  755.  
  756.   if (mask)
  757.     {
  758.       /*  make sure coordinates are in mask bounds ...
  759.        *  we need to add the layer offset to transform coords
  760.        *  into the mask coordinate system
  761.        */
  762.       x1 = CLAMP (x1, -offset_x, drawable_width (GIMP_DRAWABLE(mask))-offset_x);
  763.       y1 = CLAMP (y1, -offset_y, drawable_height(GIMP_DRAWABLE(mask))-offset_y);
  764.       x2 = CLAMP (x2, -offset_x, drawable_width (GIMP_DRAWABLE(mask))-offset_x);
  765.       y2 = CLAMP (y2, -offset_y, drawable_height(GIMP_DRAWABLE(mask))-offset_y);
  766.     }
  767.  
  768.   /*  If the calling procedure specified an undo step...  */
  769.   if (undo)
  770.     undo_push_image (gimp_drawable_gimage (drawable), drawable, x1, y1, x2, y2);
  771.  
  772.   /* configure the pixel regions
  773.    *  If an alternative to using the drawable's data as src1 was provided...
  774.    */
  775.   if (src1_tiles)
  776.     pixel_region_init (&src1PR, src1_tiles, 
  777.                x1, y1, (x2 - x1), (y2 - y1), FALSE);
  778.   else
  779.     pixel_region_init (&src1PR, drawable_data (drawable), 
  780.                x1, y1, (x2 - x1), (y2 - y1), FALSE);
  781.   pixel_region_init (&destPR, drawable_data (drawable), 
  782.              x1, y1, (x2 - x1), (y2 - y1), TRUE);
  783.   pixel_region_resize (src2PR, 
  784.                src2PR->x + (x1 - x), src2PR->y + (y1 - y), 
  785.                (x2 - x1), (y2 - y1));
  786.  
  787.   if (mask)
  788.     {
  789.       gint mx, my;
  790.  
  791.       /*  configure the mask pixel region
  792.        *  don't use x1 and y1 because they are in layer
  793.        *  coordinate system.  Need mask coordinate system
  794.        */
  795.       mx = x1 + offset_x;
  796.       my = y1 + offset_y;
  797.  
  798.       pixel_region_init (&maskPR, 
  799.              drawable_data (GIMP_DRAWABLE(mask)), 
  800.              mx, my, 
  801.              (x2 - x1), (y2 - y1), 
  802.              FALSE);
  803.       combine_regions (&src1PR, src2PR, &destPR, &maskPR, NULL,
  804.                opacity, mode, active, operation);
  805.     }
  806.   else
  807.     combine_regions (&src1PR, src2PR, &destPR, NULL, NULL,
  808.              opacity, mode, active, operation);
  809. }
  810.  
  811. /* Similar to gimp_image_apply_image but works in "replace" mode (i.e.
  812.    transparent pixels in src2 make the result transparent rather
  813.    than opaque.
  814.  
  815.    Takes an additional mask pixel region as well.
  816. */
  817. void
  818. gimp_image_replace_image (GimpImage    *gimage, 
  819.               GimpDrawable *drawable, 
  820.               PixelRegion  *src2PR,
  821.               gint          undo, 
  822.               gint          opacity,
  823.               PixelRegion  *maskPR,
  824.               gint          x, 
  825.               gint          y)
  826. {
  827.   Channel     *mask;
  828.   gint         x1, y1, x2, y2;
  829.   gint         offset_x, offset_y;
  830.   PixelRegion  src1PR, destPR;
  831.   PixelRegion  mask2PR, tempPR;
  832.   guchar      *temp_data;
  833.   gint         operation;
  834.   gint         active [MAX_CHANNELS];
  835.  
  836.   /*  get the selection mask if one exists  */
  837.   mask = (gimage_mask_is_empty (gimage)) ? NULL : gimp_image_get_mask (gimage);
  838.  
  839.   /*  configure the active channel array  */
  840.   gimp_image_get_active_channels (gimage, drawable, active);
  841.  
  842.   /*  determine what sort of operation is being attempted and
  843.    *  if it's actually legal...
  844.    */
  845.   operation = valid_combinations [drawable_type (drawable)][src2PR->bytes];
  846.   if (operation == -1)
  847.     {
  848.       g_message ("gimp_image_apply_image sent illegal parameters");
  849.       return;
  850.     }
  851.  
  852.   /*  get the layer offsets  */
  853.   drawable_offsets (drawable, &offset_x, &offset_y);
  854.  
  855.   /*  make sure the image application coordinates are within gimage bounds  */
  856.   x1 = CLAMP (x, 0, drawable_width (drawable));
  857.   y1 = CLAMP (y, 0, drawable_height (drawable));
  858.   x2 = CLAMP (x + src2PR->w, 0, drawable_width (drawable));
  859.   y2 = CLAMP (y + src2PR->h, 0, drawable_height (drawable));
  860.  
  861.   if (mask)
  862.     {
  863.       /*  make sure coordinates are in mask bounds ...
  864.        *  we need to add the layer offset to transform coords
  865.        *  into the mask coordinate system
  866.        */
  867.       x1 = CLAMP (x1, -offset_x, drawable_width (GIMP_DRAWABLE(mask))-offset_x);
  868.       y1 = CLAMP (y1, -offset_y, drawable_height(GIMP_DRAWABLE(mask))-offset_y);
  869.       x2 = CLAMP (x2, -offset_x, drawable_width (GIMP_DRAWABLE(mask))-offset_x);
  870.       y2 = CLAMP (y2, -offset_y, drawable_height(GIMP_DRAWABLE(mask))-offset_y);
  871.     }
  872.  
  873.   /*  If the calling procedure specified an undo step...  */
  874.   if (undo)
  875.     drawable_apply_image (drawable, x1, y1, x2, y2, NULL, FALSE);
  876.  
  877.   /* configure the pixel regions
  878.    *  If an alternative to using the drawable's data as src1 was provided...
  879.    */
  880.   pixel_region_init (&src1PR, drawable_data (drawable), x1, y1, (x2 - x1), (y2 - y1), FALSE);
  881.   pixel_region_init (&destPR, drawable_data (drawable), x1, y1, (x2 - x1), (y2 - y1), TRUE);
  882.   pixel_region_resize (src2PR, src2PR->x + (x1 - x), src2PR->y + (y1 - y), (x2 - x1), (y2 - y1));
  883.  
  884.   if (mask)
  885.     {
  886.       int mx, my;
  887.  
  888.       /*  configure the mask pixel region
  889.        *  don't use x1 and y1 because they are in layer
  890.        *  coordinate system.  Need mask coordinate system
  891.        */
  892.       mx = x1 + offset_x;
  893.       my = y1 + offset_y;
  894.  
  895.       pixel_region_init (&mask2PR, 
  896.              drawable_data (GIMP_DRAWABLE(mask)), 
  897.              mx, my, 
  898.              (x2 - x1), (y2 - y1), 
  899.              FALSE);
  900.  
  901.       tempPR.bytes = 1;
  902.       tempPR.x = 0;
  903.       tempPR.y = 0;
  904.       tempPR.w = x2 - x1;
  905.       tempPR.h = y2 - y1;
  906.       tempPR.rowstride = tempPR.w * tempPR.bytes;
  907.       temp_data = g_malloc (tempPR.h * tempPR.rowstride);
  908.       tempPR.data = temp_data;
  909.  
  910.       copy_region (&mask2PR, &tempPR);
  911.  
  912.       /* apparently, region operations can mutate some PR data. */
  913.       tempPR.x = 0;
  914.       tempPR.y = 0;
  915.       tempPR.w = x2 - x1;
  916.       tempPR.h = y2 - y1;
  917.       tempPR.data = temp_data;
  918.  
  919.       apply_mask_to_region (&tempPR, maskPR, OPAQUE_OPACITY);
  920.  
  921.       tempPR.x = 0;
  922.       tempPR.y = 0;
  923.       tempPR.w = x2 - x1;
  924.       tempPR.h = y2 - y1;
  925.       tempPR.data = temp_data;
  926.  
  927.       combine_regions_replace (&src1PR, src2PR, &destPR, &tempPR, NULL,
  928.                opacity, active, operation);
  929.  
  930.       g_free (temp_data);
  931.     }
  932.   else
  933.     combine_regions_replace (&src1PR, src2PR, &destPR, maskPR, NULL,
  934.              opacity, active, operation);
  935. }
  936.  
  937. /* Get rid of these! A "foreground" is an UI concept.. */
  938.  
  939. void
  940. gimp_image_get_foreground (const GimpImage *gimage, 
  941.                GimpDrawable       *drawable, 
  942.                guchar       *fg)
  943. {
  944.   guchar pfg[3];
  945.  
  946.   /*  Get the palette color  */
  947.   gimp_context_get_foreground (NULL, &pfg[0], &pfg[1], &pfg[2]);
  948.  
  949.   gimp_image_transform_color (gimage, drawable, pfg, fg, RGB);
  950. }
  951.  
  952. void
  953. gimp_image_get_background (const GimpImage *gimage, 
  954.                GimpDrawable       *drawable, 
  955.                guchar       *bg)
  956. {
  957.   guchar pbg[3];
  958.  
  959.   /*  Get the palette color  */
  960.   gimp_context_get_background (NULL, &pbg[0], &pbg[1], &pbg[2]);
  961.  
  962.   gimp_image_transform_color (gimage, drawable, pbg, bg, RGB);
  963. }
  964.  
  965. guchar *
  966. gimp_image_get_color_at (GimpImage *gimage, 
  967.              gint        x, 
  968.              gint        y)
  969. {
  970.   Tile   *tile;
  971.   guchar *src;
  972.   guchar *dest;
  973.  
  974.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  975.  
  976.   if (x < 0 || y < 0 || x >= gimage->width || y >= gimage->height)
  977.     return NULL;
  978.   
  979.   dest = g_new (unsigned char, 5);
  980.   tile = tile_manager_get_tile (gimp_image_composite (gimage), x, y,
  981.                 TRUE, FALSE);
  982.   src = tile_data_pointer (tile, x % TILE_WIDTH, y % TILE_HEIGHT);
  983.   gimp_image_get_color (gimage, gimp_image_composite_type (gimage), dest, src);
  984.  
  985.   if (GIMP_IMAGE_TYPE_HAS_ALPHA (gimp_image_composite_type (gimage)))
  986.     dest[3] = src[gimp_image_composite_bytes (gimage) - 1];
  987.   else
  988.     dest[3] = 255;
  989.  
  990.   dest[4] = 0;
  991.   tile_release (tile, FALSE);
  992.  
  993.   return dest;
  994. }
  995.  
  996. void
  997. gimp_image_get_color (const GimpImage     *gimage, 
  998.               GimpImageType       d_type,
  999.               guchar          *rgb, 
  1000.               guchar          *src)
  1001. {
  1002.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1003.  
  1004.   switch (d_type)
  1005.     {
  1006.     case RGB_GIMAGE: case RGBA_GIMAGE:
  1007.       map_to_color (0, NULL, src, rgb);
  1008.       break;
  1009.     case GRAY_GIMAGE: case GRAYA_GIMAGE:
  1010.       map_to_color (1, NULL, src, rgb);
  1011.       break;
  1012.     case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
  1013.       map_to_color (2, gimage->cmap, src, rgb);
  1014.       break;
  1015.     }
  1016. }
  1017.  
  1018. void
  1019. gimp_image_transform_color (const GimpImage         *gimage, 
  1020.                 GimpDrawable      *drawable,
  1021.                 guchar            *src, 
  1022.                 guchar            *dest, 
  1023.                 GimpImageBaseType  type)
  1024. {
  1025.   GimpImageType d_type;
  1026.  
  1027.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1028.  
  1029.   d_type = (drawable != NULL) ? drawable_type (drawable) :
  1030.     gimp_image_base_type_with_alpha (gimage);
  1031.  
  1032.   switch (type)
  1033.     {
  1034.     case RGB:
  1035.       switch (d_type)
  1036.     {
  1037.     case RGB_GIMAGE: case RGBA_GIMAGE:
  1038.       /*  Straight copy  */
  1039.       *dest++ = *src++;
  1040.       *dest++ = *src++;
  1041.       *dest++ = *src++;
  1042.       break;
  1043.     case GRAY_GIMAGE: case GRAYA_GIMAGE:
  1044.       /*  NTSC conversion  */
  1045.       *dest = INTENSITY (src[RED_PIX],
  1046.                  src[GREEN_PIX],
  1047.                  src[BLUE_PIX]);
  1048.       break;
  1049.     case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
  1050.       /*  Least squares method  */
  1051.       *dest = map_rgb_to_indexed (gimage->cmap,
  1052.                       gimage->num_cols,
  1053.                       gimage,
  1054.                       src[RED_PIX],
  1055.                       src[GREEN_PIX],
  1056.                       src[BLUE_PIX]);
  1057.       break;
  1058.     }
  1059.       break;
  1060.     case GRAY:
  1061.       switch (d_type)
  1062.     {
  1063.     case RGB_GIMAGE: case RGBA_GIMAGE:
  1064.       /*  Gray to RG&B */
  1065.       *dest++ = *src;
  1066.       *dest++ = *src;
  1067.       *dest++ = *src;
  1068.       break;
  1069.     case GRAY_GIMAGE: case GRAYA_GIMAGE:
  1070.       /*  Straight copy  */
  1071.       *dest = *src;
  1072.       break;
  1073.     case INDEXED_GIMAGE: case INDEXEDA_GIMAGE:
  1074.       /*  Least squares method  */
  1075.       *dest = map_rgb_to_indexed (gimage->cmap,
  1076.                       gimage->num_cols,
  1077.                       gimage,
  1078.                       src[GRAY_PIX],
  1079.                       src[GRAY_PIX],
  1080.                       src[GRAY_PIX]);
  1081.       break;
  1082.     }
  1083.       break;
  1084.     default:
  1085.       break;
  1086.     }
  1087. }
  1088.  
  1089. Guide*
  1090. gimp_image_add_hguide (GimpImage *gimage)
  1091. {
  1092.   Guide *guide;
  1093.  
  1094.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  1095.  
  1096.   guide = g_new (Guide, 1);
  1097.   guide->ref_count   = 0;
  1098.   guide->position    = -1;
  1099.   guide->guide_ID    = next_guide_id++;
  1100.   guide->orientation = ORIENTATION_HORIZONTAL;
  1101.  
  1102.   gimage->guides = g_list_prepend (gimage->guides, guide);
  1103.  
  1104.   return guide;
  1105. }
  1106.  
  1107. Guide*
  1108. gimp_image_add_vguide (GimpImage *gimage)
  1109. {
  1110.   Guide *guide;
  1111.  
  1112.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  1113.  
  1114.   guide = g_new (Guide, 1);
  1115.   guide->ref_count   = 0;
  1116.   guide->position    = -1;
  1117.   guide->guide_ID    = next_guide_id++;
  1118.   guide->orientation = ORIENTATION_VERTICAL;
  1119.  
  1120.   gimage->guides = g_list_prepend (gimage->guides, guide);
  1121.  
  1122.   return guide;
  1123. }
  1124.  
  1125. void
  1126. gimp_image_add_guide (GimpImage *gimage,
  1127.               Guide     *guide)
  1128. {
  1129.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1130.  
  1131.   gimage->guides = g_list_prepend (gimage->guides, guide);
  1132. }
  1133.  
  1134. void
  1135. gimp_image_remove_guide (GimpImage *gimage,
  1136.              Guide     *guide)
  1137. {
  1138.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1139.  
  1140.   gimage->guides = g_list_remove (gimage->guides, guide);
  1141. }
  1142.  
  1143. void
  1144. gimp_image_delete_guide (GimpImage *gimage,
  1145.              Guide     *guide) 
  1146. {
  1147.   guide->position = -1;
  1148.  
  1149.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1150.  
  1151.   if (guide->ref_count <= 0)
  1152.     {
  1153.       gimage->guides = g_list_remove (gimage->guides, guide);
  1154.       g_free (guide);
  1155.     }
  1156. }
  1157.  
  1158.  
  1159. GimpParasite *
  1160. gimp_image_parasite_find (const GimpImage *gimage, 
  1161.               const gchar     *name)
  1162. {
  1163.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  1164.  
  1165.   return parasite_list_find (gimage->parasites, name);
  1166. }
  1167.  
  1168. static void
  1169. list_func (gchar          *key, 
  1170.        GimpParasite   *p, 
  1171.        gchar        ***cur)
  1172. {
  1173.   *(*cur)++ = (gchar *) g_strdup (key);
  1174. }
  1175.  
  1176. gchar **
  1177. gimp_image_parasite_list (GimpImage *gimage, 
  1178.               gint      *count)
  1179. {
  1180.   gchar **list;
  1181.   gchar **cur;
  1182.  
  1183.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  1184.  
  1185.   *count = parasite_list_length (gimage->parasites);
  1186.   cur = list = g_new (gchar*, *count);
  1187.  
  1188.   parasite_list_foreach (gimage->parasites, (GHFunc) list_func, &cur);
  1189.   
  1190.   return list;
  1191. }
  1192.  
  1193. void
  1194. gimp_image_parasite_attach (GimpImage    *gimage, 
  1195.                 GimpParasite *parasite)
  1196. {
  1197.   g_return_if_fail (GIMP_IS_IMAGE (gimage) && parasite != NULL);
  1198.  
  1199.   /* only set the dirty bit manually if we can be saved and the new
  1200.      parasite differs from the current one and we aren't undoable */
  1201.   if (gimp_parasite_is_undoable (parasite))
  1202.     undo_push_image_parasite (gimage, parasite);
  1203.  
  1204.   /*  We used to push an cantundo on te stack here. This made the undo stack
  1205.       unusable (NULL on the stack) and prevented people from undoing after a 
  1206.       save (since most save plug-ins attach an undoable comment parasite).
  1207.       Now we simply attach the parasite without pushing an undo. That way it's
  1208.       undoable but does not block the undo system.   --Sven
  1209.    */
  1210.  
  1211.   parasite_list_add (gimage->parasites, parasite);
  1212.  
  1213.   if (gimp_parasite_has_flag (parasite, GIMP_PARASITE_ATTACH_PARENT))
  1214.     {
  1215.       parasite_shift_parent (parasite);
  1216.       gimp_parasite_attach (parasite);
  1217.     }
  1218. }
  1219.  
  1220. void
  1221. gimp_image_parasite_detach (GimpImage   *gimage, 
  1222.                 const gchar *parasite)
  1223. {
  1224.   GimpParasite *p;
  1225.  
  1226.   g_return_if_fail (GIMP_IS_IMAGE (gimage) && parasite != NULL);
  1227.  
  1228.   if (!(p = parasite_list_find (gimage->parasites, parasite)))
  1229.     return;
  1230.  
  1231.   if (gimp_parasite_is_undoable (p))
  1232.     undo_push_image_parasite_remove (gimage, gimp_parasite_name (p));
  1233.  
  1234.   parasite_list_remove (gimage->parasites, parasite);
  1235. }
  1236.  
  1237. Tattoo
  1238. gimp_image_get_new_tattoo (GimpImage *image)
  1239. {
  1240.   image->tattoo_state++;
  1241.   if (image->tattoo_state <= 0)
  1242.     g_warning ("Tattoo state has become corrupt (2.1 billion operation limit exceded)");
  1243.   return (image->tattoo_state);
  1244. }
  1245.  
  1246. Tattoo
  1247. gimp_image_get_tattoo_state (const GimpImage *image)
  1248. {
  1249.   return (image->tattoo_state);
  1250. }
  1251.  
  1252. gboolean
  1253. gimp_image_set_tattoo_state (GimpImage *gimage, 
  1254.                  Tattoo     val)
  1255. {
  1256.   Layer    *layer;
  1257.   GSList   *layers;
  1258.   gboolean  retval = TRUE;
  1259.   Channel  *channel;
  1260.   GSList   *channels;
  1261.   Tattoo    maxval = 0;
  1262.   Path     *pptr   = NULL;
  1263.   PathList *plist;
  1264.  
  1265.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  1266.  
  1267.   for (layers = gimage->layers; layers; layers = g_slist_next (layers))
  1268.     {
  1269.       Tattoo ltattoo;
  1270.       layer = (Layer *) layers->data;
  1271.       
  1272.       ltattoo = layer_get_tattoo (layer);
  1273.       if (ltattoo > maxval)
  1274.     maxval = ltattoo;
  1275.       if (gimp_image_get_channel_by_tattoo (gimage, ltattoo) != NULL)
  1276.     {
  1277.       retval = FALSE; /* Oopps duplicated tattoo in channel */
  1278.     }
  1279.  
  1280.       /* Now check path an't got this tattoo */
  1281.       if (path_get_path_by_tattoo (gimage, ltattoo) != NULL)
  1282.     {
  1283.       retval = FALSE; /* Oopps duplicated tattoo in layer */
  1284.     }
  1285.     }
  1286.  
  1287.   /* Now check that the paths channel tattoos don't overlap */
  1288.   for (channels = gimage->channels; 
  1289.        channels; 
  1290.        channels = g_slist_next (channels))
  1291.     {
  1292.       Tattoo ctattoo;
  1293.       channel = (Channel *) channels->data;
  1294.       
  1295.       ctattoo = channel_get_tattoo (channel);
  1296.       if (ctattoo > maxval)
  1297.     maxval = ctattoo;
  1298.       /* Now check path an't got this tattoo */
  1299.       if (path_get_path_by_tattoo (gimage, ctattoo) != NULL)
  1300.     {
  1301.       retval = FALSE; /* Oopps duplicated tattoo in layer */
  1302.     }
  1303.     }
  1304.  
  1305.   /* Find the max tatto value in the paths */
  1306.   plist = gimage->paths;
  1307.       
  1308.   if (plist && plist->bz_paths)
  1309.     {
  1310.       Tattoo  ptattoo;
  1311.       GSList *pl = plist->bz_paths;
  1312.  
  1313.       while (pl)
  1314.     {
  1315.       pptr = pl->data;
  1316.  
  1317.       ptattoo = path_get_tattoo (pptr);
  1318.       
  1319.       if (ptattoo > maxval)
  1320.         maxval = ptattoo;
  1321.       
  1322.       pl = pl->next;
  1323.     }
  1324.     }
  1325.  
  1326.   if (val < maxval)
  1327.     retval = FALSE;
  1328.   /* Must check the state is valid */
  1329.   if (retval == TRUE)
  1330.     gimage->tattoo_state = val;
  1331.  
  1332.   return retval;
  1333. }
  1334.  
  1335. void
  1336. gimp_image_colormap_changed (GimpImage *gimage, 
  1337.                  gint       col)
  1338. {
  1339.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1340.   g_return_if_fail (col < gimage->num_cols);
  1341.  
  1342.   gtk_signal_emit (GTK_OBJECT (gimage),
  1343.            gimp_image_signals[COLORMAP_CHANGED],
  1344.            col);
  1345. }
  1346.  
  1347. void
  1348. gimp_image_set_paths (GimpImage *gimage,
  1349.               PathList  *paths)
  1350. {
  1351.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1352.  
  1353.   gimage->paths = paths;
  1354. }
  1355.  
  1356. PathList *
  1357. gimp_image_get_paths (const GimpImage *gimage)
  1358. {
  1359.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  1360.  
  1361.   return gimage->paths;
  1362. }
  1363.     
  1364. /************************************************************/
  1365. /*  Projection functions                                    */
  1366. /************************************************************/
  1367.  
  1368. static void
  1369. project_intensity (GimpImage   *gimage, 
  1370.            Layer       *layer,
  1371.            PixelRegion *src, 
  1372.            PixelRegion *dest, 
  1373.            PixelRegion *mask)
  1374. {
  1375.   if (! gimage->construct_flag)
  1376.     initial_region (src, dest, mask, NULL, layer->opacity,
  1377.             layer->mode, gimage->visible, INITIAL_INTENSITY);
  1378.   else
  1379.     combine_regions (dest, src, dest, mask, NULL, layer->opacity,
  1380.              layer->mode, gimage->visible, COMBINE_INTEN_A_INTEN);
  1381. }
  1382.  
  1383. static void
  1384. project_intensity_alpha (GimpImage   *gimage, 
  1385.              Layer       *layer,
  1386.              PixelRegion *src, 
  1387.              PixelRegion *dest,
  1388.              PixelRegion *mask)
  1389. {
  1390.   if (! gimage->construct_flag)
  1391.     initial_region (src, dest, mask, NULL, layer->opacity,
  1392.             layer->mode, gimage->visible, INITIAL_INTENSITY_ALPHA);
  1393.   else
  1394.     combine_regions (dest, src, dest, mask, NULL, layer->opacity,
  1395.              layer->mode, gimage->visible, COMBINE_INTEN_A_INTEN_A);
  1396. }
  1397.  
  1398. static void
  1399. project_indexed (GimpImage   *gimage, 
  1400.          Layer       *layer,
  1401.          PixelRegion *src, 
  1402.          PixelRegion *dest)
  1403. {
  1404.   if (! gimage->construct_flag)
  1405.     initial_region (src, dest, NULL, gimage->cmap, layer->opacity,
  1406.             layer->mode, gimage->visible, INITIAL_INDEXED);
  1407.   else
  1408.     g_message ("Unable to project indexed image.");
  1409. }
  1410.  
  1411. static void
  1412. project_indexed_alpha (GimpImage   *gimage, 
  1413.                Layer       *layer,
  1414.                PixelRegion *src, 
  1415.                PixelRegion *dest,
  1416.                PixelRegion *mask)
  1417. {
  1418.   if (! gimage->construct_flag)
  1419.     initial_region (src, dest, mask, gimage->cmap, layer->opacity,
  1420.             layer->mode, gimage->visible, INITIAL_INDEXED_ALPHA);
  1421.   else
  1422.     combine_regions (dest, src, dest, mask, gimage->cmap, layer->opacity,
  1423.              layer->mode, gimage->visible, COMBINE_INTEN_A_INDEXED_A);
  1424. }
  1425.  
  1426. static void
  1427. project_channel (GimpImage   *gimage, 
  1428.          Channel     *channel,
  1429.          PixelRegion *src, 
  1430.          PixelRegion *src2)
  1431. {
  1432.   gint type;
  1433.  
  1434.   if (! gimage->construct_flag)
  1435.     {
  1436.       type = (channel->show_masked) ?
  1437.     INITIAL_CHANNEL_MASK : INITIAL_CHANNEL_SELECTION;
  1438.       initial_region (src2, src, NULL, channel->col, channel->opacity,
  1439.               NORMAL_MODE, NULL, type);
  1440.     }
  1441.   else
  1442.     {
  1443.       type = (channel->show_masked) ?
  1444.     COMBINE_INTEN_A_CHANNEL_MASK : COMBINE_INTEN_A_CHANNEL_SELECTION;
  1445.       combine_regions (src, src2, src, NULL, channel->col, channel->opacity,
  1446.                NORMAL_MODE, NULL, type);
  1447.     }
  1448. }
  1449.  
  1450. /************************************************************/
  1451. /*  Layer/Channel functions                                 */
  1452. /************************************************************/
  1453.  
  1454. static void
  1455. gimp_image_free_layers (GimpImage *gimage)
  1456. {
  1457.   GSList *list;
  1458.   Layer  *layer;
  1459.  
  1460.   for (list = gimage->layers; list; list = g_slist_next (list))
  1461.     {
  1462.       layer = (Layer *) list->data;
  1463.       layer_unref (layer);
  1464.     }
  1465.   g_slist_free (gimage->layers);
  1466.   g_slist_free (gimage->layer_stack);
  1467. }
  1468.  
  1469.  
  1470. static void
  1471. gimp_image_free_channels (GimpImage *gimage)
  1472. {
  1473.   GSList  *list;
  1474.   Channel *channel;
  1475.  
  1476.   for (list = gimage->channels; list; list = g_slist_next (list))
  1477.     {
  1478.       channel = (Channel *) list->data;
  1479.       channel_unref (channel);
  1480.     }
  1481.   g_slist_free (gimage->channels);
  1482. }
  1483.  
  1484.  
  1485. static void
  1486. gimp_image_construct_layers (GimpImage *gimage, 
  1487.                  gint       x, 
  1488.                  gint       y, 
  1489.                  gint       w, 
  1490.                  gint       h)
  1491. {
  1492.   Layer       *layer;
  1493.   gint         x1, y1, x2, y2;
  1494.   PixelRegion  src1PR, src2PR, maskPR;
  1495.   PixelRegion * mask;
  1496.   GSList      *list;
  1497.   GSList      *reverse_list = NULL;
  1498.   gint         off_x;
  1499.   gint         off_y;
  1500.  
  1501.   /*  composite the floating selection if it exists  */
  1502.   if ((layer = gimp_image_floating_sel (gimage)))
  1503.     floating_sel_composite (layer, x, y, w, h, FALSE);
  1504.  
  1505.   /* Note added by Raph Levien, 27 Jan 1998
  1506.  
  1507.      This looks it was intended as an optimization, but it seems to
  1508.      have correctness problems. In particular, if all channels are
  1509.      turned off, the screen simply does not update the projected
  1510.      image. It should be black. Turning off this optimization seems to
  1511.      restore correct behavior. At some future point, it may be
  1512.      desirable to turn the optimization back on.
  1513.  
  1514.      */
  1515. #if 0
  1516.   /*  If all channels are not visible, simply return  */
  1517.   switch (gimp_image_base_type (gimage))
  1518.     {
  1519.     case RGB:
  1520.       if (! gimp_image_get_component_visible (gimage, RED_CHANNEL) &&
  1521.       ! gimp_image_get_component_visible (gimage, GREEN_CHANNEL) &&
  1522.       ! gimp_image_get_component_visible (gimage, BLUE_CHANNEL))
  1523.     return;
  1524.       break;
  1525.     case GRAY:
  1526.       if (! gimp_image_get_component_visible (gimage, GRAY_CHANNEL))
  1527.     return;
  1528.       break;
  1529.     case INDEXED:
  1530.       if (! gimp_image_get_component_visible (gimage, INDEXED_CHANNEL))
  1531.     return;
  1532.       break;
  1533.     }
  1534. #endif
  1535.  
  1536.   for (list = gimage->layers; list; list = g_slist_next (list))
  1537.     {
  1538.       layer = (Layer *) list->data;
  1539.  
  1540.       /*  only add layers that are visible and not floating selections 
  1541.       to the list  */
  1542.       if (!layer_is_floating_sel (layer) && 
  1543.       drawable_visible (GIMP_DRAWABLE(layer)))
  1544.     reverse_list = g_slist_prepend (reverse_list, layer);
  1545.     }
  1546.  
  1547.   list = reverse_list;
  1548.  
  1549.   while (list)
  1550.     {
  1551.       layer = (Layer *) list->data;
  1552.       drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
  1553.  
  1554.       x1 = CLAMP (off_x, x, x + w);
  1555.       y1 = CLAMP (off_y, y, y + h);
  1556.       x2 = CLAMP (off_x + drawable_width (GIMP_DRAWABLE(layer)), x, x + w);
  1557.       y2 = CLAMP (off_y + drawable_height (GIMP_DRAWABLE(layer)), y, y + h);
  1558.  
  1559.       /* configure the pixel regions  */
  1560.       pixel_region_init (&src1PR, gimp_image_projection (gimage), 
  1561.              x1, y1, (x2 - x1), (y2 - y1), 
  1562.              TRUE);
  1563.  
  1564.       /*  If we're showing the layer mask instead of the layer...  */
  1565.       if (layer->mask && layer->show_mask)
  1566.     {
  1567.       pixel_region_init (&src2PR, 
  1568.                  drawable_data (GIMP_DRAWABLE(layer->mask)),
  1569.                  (x1 - off_x), (y1 - off_y),
  1570.                  (x2 - x1), (y2 - y1), FALSE);
  1571.  
  1572.       copy_gray_to_region (&src2PR, &src1PR);
  1573.     }
  1574.       /*  Otherwise, normal  */
  1575.       else
  1576.     {
  1577.       pixel_region_init (&src2PR, 
  1578.                  drawable_data (GIMP_DRAWABLE (layer)),
  1579.                  (x1 - off_x), (y1 - off_y),
  1580.                  (x2 - x1), (y2 - y1), FALSE);
  1581.  
  1582.       if (layer->mask && layer->apply_mask)
  1583.         {
  1584.           pixel_region_init (&maskPR, 
  1585.                  drawable_data (GIMP_DRAWABLE (layer->mask)),
  1586.                  (x1 - off_x), (y1 - off_y),
  1587.                  (x2 - x1), (y2 - y1), FALSE);
  1588.           mask = &maskPR;
  1589.         }
  1590.       else
  1591.         mask = NULL;
  1592.  
  1593.       /*  Based on the type of the layer, project the layer onto the
  1594.        *  projection image...
  1595.        */
  1596.       switch (drawable_type (GIMP_DRAWABLE(layer)))
  1597.         {
  1598.         case RGB_GIMAGE: case GRAY_GIMAGE:
  1599.           /* no mask possible */
  1600.           project_intensity (gimage, layer, &src2PR, &src1PR, mask);
  1601.           break;
  1602.  
  1603.         case RGBA_GIMAGE: case GRAYA_GIMAGE:
  1604.           project_intensity_alpha (gimage, layer, &src2PR, &src1PR, mask);
  1605.           break;
  1606.  
  1607.         case INDEXED_GIMAGE:
  1608.           /* no mask possible */
  1609.           project_indexed (gimage, layer, &src2PR, &src1PR);
  1610.           break;
  1611.  
  1612.         case INDEXEDA_GIMAGE:
  1613.           project_indexed_alpha (gimage, layer, &src2PR, &src1PR, mask);
  1614.           break;
  1615.  
  1616.         default:
  1617.           break;
  1618.         }
  1619.     }
  1620.       gimage->construct_flag = 1;  /*  something was projected  */
  1621.  
  1622.       list = g_slist_next (list);
  1623.     }
  1624.  
  1625.   g_slist_free (reverse_list);
  1626. }
  1627.  
  1628. static void
  1629. gimp_image_construct_channels (GimpImage *gimage, 
  1630.                    gint       x, 
  1631.                    gint       y, 
  1632.                    gint       w, 
  1633.                    gint       h)
  1634. {
  1635.   Channel     *channel;
  1636.   PixelRegion  src1PR, src2PR;
  1637.   GSList      *list;
  1638.   GSList      *reverse_list = NULL;
  1639.  
  1640.   /*  reverse the channel list  */
  1641.   for (list = gimage->channels; list; list = g_slist_next (list))
  1642.     reverse_list = g_slist_prepend (reverse_list, list->data);
  1643.  
  1644.   list = reverse_list;
  1645.  
  1646.   while (list)
  1647.     {
  1648.       channel = (Channel *) list->data;
  1649.  
  1650.       if (drawable_visible (GIMP_DRAWABLE (channel)))
  1651.     {
  1652.       /* configure the pixel regions  */
  1653.       pixel_region_init (&src1PR, gimp_image_projection (gimage), 
  1654.                  x, y, w, h, 
  1655.                  TRUE);
  1656.       pixel_region_init (&src2PR, drawable_data (GIMP_DRAWABLE(channel)), 
  1657.                  x, y, w, h, 
  1658.                  FALSE);
  1659.  
  1660.       project_channel (gimage, channel, &src1PR, &src2PR);
  1661.  
  1662.       gimage->construct_flag = 1;
  1663.     }
  1664.  
  1665.       list = g_slist_next (list);
  1666.     }
  1667.  
  1668.   g_slist_free (reverse_list);
  1669. }
  1670.  
  1671. static void
  1672. gimp_image_initialize_projection (GimpImage *gimage, 
  1673.                   gint       x, 
  1674.                   gint       y, 
  1675.                   gint       w, 
  1676.                   gint       h)
  1677. {
  1678.   GSList      *list;
  1679.   Layer       *layer;
  1680.   gint         coverage = 0;
  1681.   PixelRegion  PR;
  1682.   guchar       clear[4] = { 0, 0, 0, 0 };
  1683.  
  1684.   /*  this function determines whether a visible layer
  1685.    *  provides complete coverage over the image.  If not,
  1686.    *  the projection is initialized to transparent
  1687.    */
  1688.   
  1689.   for (list = gimage->layers; list; list = g_slist_next (list))
  1690.     {
  1691.       gint off_x, off_y;
  1692.  
  1693.       layer = (Layer *) list->data;
  1694.       drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
  1695.  
  1696.       if (drawable_visible (GIMP_DRAWABLE(layer)) &&
  1697.       ! layer_has_alpha (layer) &&
  1698.       (off_x <= x) &&
  1699.       (off_y <= y) &&
  1700.       (off_x + drawable_width (GIMP_DRAWABLE(layer)) >= x + w) &&
  1701.       (off_y + drawable_height (GIMP_DRAWABLE(layer)) >= y + h))
  1702.     {
  1703.       coverage = 1;
  1704.       break;
  1705.     }
  1706.     }
  1707.  
  1708.   if (!coverage)
  1709.     {
  1710.       pixel_region_init (&PR, gimp_image_projection (gimage), 
  1711.              x, y, w, h, TRUE);
  1712.       color_region (&PR, clear);
  1713.     }
  1714. }
  1715.  
  1716. static void
  1717. gimp_image_get_active_channels (GimpImage    *gimage, 
  1718.                 GimpDrawable *drawable, 
  1719.                 gint         *active)
  1720. {
  1721.   Layer *layer;
  1722.   gint   i;
  1723.  
  1724.   /*  first, blindly copy the gimage active channels  */
  1725.   for (i = 0; i < MAX_CHANNELS; i++)
  1726.     active[i] = gimage->active[i];
  1727.  
  1728.   /*  If the drawable is a channel (a saved selection, etc.)
  1729.    *  make sure that the alpha channel is not valid
  1730.    */
  1731.   if (GIMP_IS_CHANNEL (drawable))
  1732.     active[ALPHA_G_PIX] = 0;  /*  no alpha values in channels  */
  1733.   else
  1734.     {
  1735.       /*  otherwise, check whether preserve transparency is
  1736.        *  enabled in the layer and if the layer has alpha
  1737.        */
  1738.       if (GIMP_IS_LAYER (drawable))
  1739.     {
  1740.       layer = GIMP_LAYER (drawable);
  1741.       if (layer_has_alpha (layer) && layer->preserve_trans)
  1742.         active[drawable_bytes (drawable) - 1] = 0;
  1743.     }
  1744.     }
  1745. }
  1746.  
  1747. void
  1748. gimp_image_construct (GimpImage *gimage, 
  1749.               gint       x, 
  1750.               gint       y, 
  1751.               gint       w, 
  1752.               gint       h,
  1753.               gboolean   can_use_cowproject)
  1754. {
  1755.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1756.  
  1757. #if 0
  1758.   gint xoff;
  1759.   gint yoff;
  1760.   
  1761.   /*  set the construct flag, used to determine if anything
  1762.    *  has been written to the gimage raw image yet.
  1763.    */
  1764.   gimage->construct_flag = 0;
  1765.  
  1766.   if (gimage->layers)
  1767.     {
  1768.       gimp_drawable_offsets (GIMP_DRAWABLE((Layer*)(gimage->layers->data)),
  1769.                  &xoff, &yoff);
  1770.     }
  1771.  
  1772.   if (/*can_use_cowproject &&*/
  1773.       (gimage->layers) &&                         /* There's a layer.      */
  1774.       (!g_slist_next(gimage->layers)) &&          /* It's the only layer.  */
  1775.       (layer_has_alpha((Layer*)(gimage->layers->data))) && /* It's !flat.  */
  1776.                                                   /* It's visible.         */
  1777.       (drawable_visible (GIMP_DRAWABLE ((Layer*)(gimage->layers->data)))) &&
  1778.       (drawable_width (GIMP_DRAWABLE ((Layer*)(gimage->layers->data))) ==
  1779.        gimage->width) &&
  1780.       (drawable_height (GIMP_DRAWABLE ((Layer*)(gimage->layers->data))) ==
  1781.        gimage->height) &&                         /* Covers all.           */
  1782.                                                   /* Not indexed.          */
  1783.       (!drawable_indexed (GIMP_DRAWABLE ((Layer*)(gimage->layers->data)))) &&
  1784.       (((Layer*)(gimage->layers->data))->opacity == OPAQUE_OPACITY) /*opaq */
  1785.       )
  1786.     {
  1787.       gint xoff;
  1788.       gint yoff;
  1789.       
  1790.       gimp_drawable_offsets (GIMP_DRAWABLE ((Layer*)(gimage->layers->data)),
  1791.                  &xoff, &yoff);
  1792.  
  1793.  
  1794.       if ((xoff==0) && (yoff==0)) /* Starts at 0,0         */
  1795.     {
  1796.       PixelRegion srcPR, destPR;
  1797.       gpointer    pr;
  1798.     
  1799.       g_warning("Can use cow-projection hack.  Yay!");
  1800.  
  1801.       pixel_region_init (&srcPR, gimp_drawable_data
  1802.                  (GIMP_DRAWABLE
  1803.                   ((Layer*)(gimage->layers->data))),
  1804.                  x, y, w,h, FALSE);
  1805.       pixel_region_init (&destPR,
  1806.                  gimp_image_projection (gimage),
  1807.                  x, y, w,h, TRUE);
  1808.  
  1809.       for (pr = pixel_regions_register (2, &srcPR, &destPR);
  1810.            pr != NULL;
  1811.            pr = pixel_regions_process (pr))
  1812.         {
  1813.           tile_manager_map_over_tile (destPR.tiles,
  1814.                       destPR.curtile, srcPR.curtile);
  1815.         }
  1816.  
  1817.       gimage->construct_flag = 1;
  1818.       gimp_image_construct_channels (gimage, x, y, w, h);
  1819.  
  1820.       return;
  1821.     }
  1822.     }
  1823. #else
  1824.   gimage->construct_flag = 0;
  1825. #endif
  1826.   
  1827.   /*  First, determine if the projection image needs to be
  1828.    *  initialized--this is the case when there are no visible
  1829.    *  layers that cover the entire canvas--either because layers
  1830.    *  are offset or only a floating selection is visible
  1831.    */
  1832.   gimp_image_initialize_projection (gimage, x, y, w, h);
  1833.   
  1834.   /*  call functions which process the list of layers and
  1835.    *  the list of channels
  1836.    */
  1837.   gimp_image_construct_layers (gimage, x, y, w, h);
  1838.   gimp_image_construct_channels (gimage, x, y, w, h);
  1839. }
  1840.  
  1841. void
  1842. gimp_image_invalidate_without_render (GimpImage *gimage, 
  1843.                       gint       x,
  1844.                       gint       y,
  1845.                       gint       w,
  1846.                       gint       h,
  1847.                       gint       x1,
  1848.                       gint       y1,
  1849.                       gint       x2,
  1850.                       gint       y2)
  1851. {
  1852.   Tile        *tile;
  1853.   TileManager *tm;
  1854.   gint         i, j;
  1855.  
  1856.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1857.  
  1858.   tm = gimp_image_projection (gimage);
  1859.  
  1860.   /*  invalidate all tiles which are located outside of the displayed area
  1861.    *   all tiles inside the displayed area are constructed.
  1862.    */
  1863.   for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
  1864.     for (j = x; j < (x + w); j += (TILE_WIDTH - (j % TILE_WIDTH)))
  1865.       {
  1866.     tile = tile_manager_get_tile (tm, j, i, FALSE, FALSE);
  1867.  
  1868.         /*  check if the tile is outside the bounds  */
  1869.         if ((MIN ((j + tile_ewidth(tile)), x2) - MAX (j, x1)) <= 0)
  1870.           {
  1871.             tile_invalidate_tile (&tile, tm, j, i);
  1872.           }
  1873.         else if (MIN ((i + tile_eheight(tile)), y2) - MAX (i, y1) <= 0)
  1874.           {
  1875.             tile_invalidate_tile (&tile, tm, j, i);
  1876.           }
  1877.       }
  1878. }
  1879.  
  1880. void
  1881. gimp_image_invalidate (GimpImage *gimage, 
  1882.                gint       x,
  1883.                gint       y,
  1884.                gint       w,
  1885.                gint       h,
  1886.                gint       x1,
  1887.                gint       y1,
  1888.                gint       x2,
  1889.                gint       y2)
  1890. {
  1891.   Tile        *tile;
  1892.   TileManager *tm;
  1893.   gint         i, j;
  1894.   gint         startx, starty;
  1895.   gint         endx, endy;
  1896.   gint         tilex, tiley;
  1897.  
  1898.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  1899.  
  1900.   tm = gimp_image_projection (gimage);
  1901.  
  1902.   startx = x;
  1903.   starty = y;
  1904.   endx   = x + w;
  1905.   endy   = y + h;
  1906.  
  1907.   /*  invalidate all tiles which are located outside of the displayed area
  1908.    *   all tiles inside the displayed area are constructed.
  1909.    */
  1910.   for (i = y; i < (y + h); i += (TILE_HEIGHT - (i % TILE_HEIGHT)))
  1911.     for (j = x; j < (x + w); j += (TILE_WIDTH - (j % TILE_WIDTH)))
  1912.       {
  1913.     tile = tile_manager_get_tile (tm, j, i, FALSE, FALSE);
  1914.  
  1915.         /*  check if the tile is outside the bounds  */
  1916.         if ((MIN ((j + tile_ewidth(tile)), x2) - MAX (j, x1)) <= 0)
  1917.           {
  1918.             tile_invalidate_tile (&tile, tm, j, i);
  1919.             if (j < x1)
  1920.               startx = MAX (startx, (j + tile_ewidth(tile)));
  1921.             else
  1922.               endx = MIN (endx, j);
  1923.           }
  1924.         else if (MIN ((i + tile_eheight(tile)), y2) - MAX (i, y1) <= 0)
  1925.           {
  1926.             tile_invalidate_tile (&tile, tm, j, i);
  1927.             if (i < y1)
  1928.               starty = MAX (starty, (i + tile_eheight(tile)));
  1929.             else
  1930.               endy = MIN (endy, i);
  1931.           }
  1932.         else
  1933.           {
  1934.             /*  If the tile is not valid, make sure we get the entire tile
  1935.              *   in the construction extents
  1936.              */
  1937.             if (tile_is_valid(tile) == FALSE)
  1938.               {
  1939.                 tilex = j - (j % TILE_WIDTH);
  1940.                 tiley = i - (i % TILE_HEIGHT);
  1941.                 
  1942.                 startx = MIN (startx, tilex);
  1943.                 endx = MAX (endx, tilex + tile_ewidth(tile));
  1944.                 starty = MIN (starty, tiley);
  1945.                 endy = MAX (endy, tiley + tile_eheight(tile));
  1946.                 
  1947.                 tile_mark_valid (tile); /* hmmmmmmm..... */
  1948.               }
  1949.           }
  1950.       }
  1951.  
  1952.   if ((endx - startx) > 0 && (endy - starty) > 0)
  1953.     gimp_image_construct (gimage, 
  1954.               startx, starty, 
  1955.               (endx - startx), (endy - starty), 
  1956.               TRUE);
  1957. }
  1958.  
  1959. void
  1960. gimp_image_validate (TileManager *tm, 
  1961.              Tile        *tile)
  1962. {
  1963.   GimpImage *gimage;
  1964.   gint       x, y;
  1965.   gint       w, h;
  1966.  
  1967.   gimp_add_busy_cursors_until_idle ();
  1968.  
  1969.   /*  Get the gimage from the tilemanager  */
  1970.   gimage = (GimpImage *) tile_manager_get_user_data (tm);
  1971.  
  1972.   /*  Find the coordinates of this tile  */
  1973.   tile_manager_get_tile_coordinates (tm, tile, &x, &y);
  1974.   w = tile_ewidth  (tile);
  1975.   h = tile_eheight (tile);
  1976.   
  1977.   gimp_image_construct (gimage, x, y, w, h, FALSE);
  1978. }
  1979.  
  1980. gint
  1981. gimp_image_get_layer_index (GimpImage *gimage, 
  1982.                 Layer     *layer_arg)
  1983. {
  1984.   Layer  *layer;
  1985.   GSList *layers;
  1986.   gint    index;
  1987.  
  1988.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  1989.  
  1990.   for (layers = gimage->layers, index = 0; 
  1991.        layers;
  1992.        layers = g_slist_next (layers), index++)
  1993.     {
  1994.       layer = (Layer *) layers->data;
  1995.       if (layer == layer_arg)
  1996.     return index;
  1997.     }
  1998.  
  1999.   return -1;
  2000. }
  2001.  
  2002. gint
  2003. gimp_image_get_channel_index (GimpImage *gimage, 
  2004.                   Channel   *channel_ID)
  2005. {
  2006.   Channel *channel;
  2007.   GSList  *channels;
  2008.   gint     index;
  2009.  
  2010.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  2011.  
  2012.   for (channels = gimage->channels, index = 0;
  2013.        channels;
  2014.        channels = g_slist_next (channels), index++)
  2015.     {
  2016.       channel = (Channel *) channels->data;
  2017.       if (channel == channel_ID)
  2018.     return index;
  2019.     }
  2020.  
  2021.   return -1;
  2022. }
  2023.  
  2024. Layer *
  2025. gimp_image_get_active_layer (GimpImage *gimage)
  2026. {
  2027.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2028.  
  2029.   return gimage->active_layer;
  2030. }
  2031.  
  2032. Channel *
  2033. gimp_image_get_active_channel (GimpImage *gimage)
  2034. {
  2035.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2036.  
  2037.   return gimage->active_channel;
  2038. }
  2039.  
  2040. Layer *
  2041. gimp_image_get_layer_by_tattoo (GimpImage *gimage, 
  2042.                 Tattoo     tattoo)
  2043. {
  2044.   Layer  *layer;
  2045.   GSList *layers;
  2046.  
  2047.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2048.  
  2049.   for (layers = gimage->layers; 
  2050.        layers; 
  2051.        layers = g_slist_next (layers))
  2052.     {
  2053.       layer = (Layer *) layers->data;
  2054.       if (layer_get_tattoo (layer) == tattoo)
  2055.     return layer;
  2056.     }
  2057.  
  2058.   return NULL;
  2059. }
  2060.  
  2061. Channel *
  2062. gimp_image_get_channel_by_tattoo (GimpImage *gimage, 
  2063.                   Tattoo     tattoo)
  2064. {
  2065.   Channel *channel;
  2066.   GSList  *channels;
  2067.  
  2068.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2069.  
  2070.   for (channels = gimage->channels; 
  2071.        channels; 
  2072.        channels = g_slist_next (channels))
  2073.     {
  2074.       channel = (Channel *) channels->data;
  2075.       if (channel_get_tattoo (channel) == tattoo)
  2076.     return channel;
  2077.     }
  2078.  
  2079.   return NULL;
  2080. }
  2081.  
  2082. Channel *
  2083. gimp_image_get_channel_by_name (GimpImage *gimage, 
  2084.                 char      *name)
  2085. {
  2086.   Channel *channel;
  2087.   GSList  *channels;
  2088.  
  2089.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2090.  
  2091.   for (channels = gimage->channels; 
  2092.        channels; 
  2093.        channels = g_slist_next (channels))
  2094.     {
  2095.       channel = (Channel *) channels->data;
  2096.       if (! strcmp(channel_get_name (channel),name) )
  2097.       return channel;
  2098.     }
  2099.  
  2100.   return NULL;
  2101. }
  2102.  
  2103. gint
  2104. gimp_image_get_component_active (GimpImage   *gimage, 
  2105.                  ChannelType  type)
  2106. {
  2107.   /*  No sanity checking here...  */
  2108.   switch (type)
  2109.     {
  2110.     case RED_CHANNEL:     return gimage->active[RED_PIX]; break;
  2111.     case GREEN_CHANNEL:   return gimage->active[GREEN_PIX]; break;
  2112.     case BLUE_CHANNEL:    return gimage->active[BLUE_PIX]; break;
  2113.     case GRAY_CHANNEL:    return gimage->active[GRAY_PIX]; break;
  2114.     case INDEXED_CHANNEL: return gimage->active[INDEXED_PIX]; break;
  2115.     default: return FALSE; break;
  2116.     }
  2117. }
  2118.  
  2119. gint
  2120. gimp_image_get_component_visible (GimpImage   *gimage, 
  2121.                   ChannelType  type)
  2122. {
  2123.   /*  No sanity checking here...  */
  2124.   switch (type)
  2125.     {
  2126.     case RED_CHANNEL:     return gimage->visible[RED_PIX]; break;
  2127.     case GREEN_CHANNEL:   return gimage->visible[GREEN_PIX]; break;
  2128.     case BLUE_CHANNEL:    return gimage->visible[BLUE_PIX]; break;
  2129.     case GRAY_CHANNEL:    return gimage->visible[GRAY_PIX]; break;
  2130.     case INDEXED_CHANNEL: return gimage->visible[INDEXED_PIX]; break;
  2131.     default: return FALSE; break;
  2132.     }
  2133. }
  2134.  
  2135. Channel *
  2136. gimp_image_get_mask (GimpImage *gimage)
  2137. {
  2138.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2139.  
  2140.   return gimage->selection_mask;
  2141. }
  2142.  
  2143. gboolean
  2144. gimp_image_layer_boundary (GimpImage  *gimage, 
  2145.                BoundSeg  **segs, 
  2146.                int        *num_segs)
  2147. {
  2148.   Layer *layer;
  2149.  
  2150.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  2151.  
  2152.   /*  The second boundary corresponds to the active layer's
  2153.    *  perimeter...
  2154.    */
  2155.   if ((layer = gimage->active_layer))
  2156.     {
  2157.       *segs = layer_boundary (layer, num_segs);
  2158.       return TRUE;
  2159.     }
  2160.   else
  2161.     {
  2162.       *segs = NULL;
  2163.       *num_segs = 0;
  2164.       return FALSE;
  2165.     }
  2166. }
  2167.  
  2168. Layer *
  2169. gimp_image_set_active_layer (GimpImage *gimage, 
  2170.                  Layer     *layer)
  2171. {
  2172.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2173.  
  2174.   /*  First, find the layer in the gimage
  2175.    *  If it isn't valid, find the first layer that is
  2176.    */
  2177.   if (gimp_image_get_layer_index (gimage, layer) == -1)
  2178.     {
  2179.       if (! gimage->layers)
  2180.     return NULL;
  2181.       layer = (Layer *) gimage->layers->data;
  2182.     }
  2183.  
  2184.   if (! layer)
  2185.     return NULL;
  2186.  
  2187.   /*  Configure the layer stack to reflect this change  */
  2188.   gimage->layer_stack = g_slist_remove (gimage->layer_stack, (void *) layer);
  2189.   gimage->layer_stack = g_slist_prepend (gimage->layer_stack, (void *) layer);
  2190.  
  2191.   /*  invalidate the selection boundary because of a layer modification  */
  2192.   layer_invalidate_boundary (layer);
  2193.  
  2194.   /*  Set the active layer  */
  2195.   gimage->active_layer   = layer;
  2196.   gimage->active_channel = NULL;
  2197.  
  2198.   /*  return the layer  */
  2199.   return layer;
  2200. }
  2201.  
  2202. Channel *
  2203. gimp_image_set_active_channel (GimpImage *gimage, 
  2204.                    Channel   *channel)
  2205. {
  2206.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2207.  
  2208.   /*  Not if there is a floating selection  */
  2209.   if (gimp_image_floating_sel (gimage))
  2210.     return NULL;
  2211.  
  2212.   /*  First, find the channel
  2213.    *  If it doesn't exist, find the first channel that does
  2214.    */
  2215.   if (! channel) 
  2216.     {
  2217.       if (! gimage->channels)
  2218.     {
  2219.       gimage->active_channel = NULL;
  2220.       return NULL;
  2221.     }
  2222.       channel = (Channel *) gimage->channels->data;
  2223.     }
  2224.  
  2225.   /*  Set the active channel  */
  2226.   gimage->active_channel = channel;
  2227.  
  2228.   /*  return the channel  */
  2229.   return channel;
  2230. }
  2231.  
  2232. Channel *
  2233. gimp_image_unset_active_channel (GimpImage *gimage)
  2234. {
  2235.   Channel *channel;
  2236.  
  2237.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2238.  
  2239.   /*  make sure there is an active channel  */
  2240.   if (! (channel = gimage->active_channel))
  2241.     return NULL;
  2242.  
  2243.   /*  Set the active channel  */
  2244.   gimage->active_channel = NULL;
  2245.  
  2246.   return channel;
  2247. }
  2248.  
  2249. void
  2250. gimp_image_set_component_active (GimpImage   *gimage, 
  2251.                  ChannelType  type, 
  2252.                  gint         value)
  2253. {
  2254.   /*  No sanity checking here...  */
  2255.   switch (type)
  2256.     {
  2257.     case RED_CHANNEL:     gimage->active[RED_PIX] = value; break;
  2258.     case GREEN_CHANNEL:   gimage->active[GREEN_PIX] = value; break;
  2259.     case BLUE_CHANNEL:    gimage->active[BLUE_PIX] = value; break;
  2260.     case GRAY_CHANNEL:    gimage->active[GRAY_PIX] = value; break;
  2261.     case INDEXED_CHANNEL: gimage->active[INDEXED_PIX] = value; break;
  2262.     case AUXILLARY_CHANNEL: break;
  2263.     }
  2264.  
  2265.   /*  If there is an active channel and we mess with the components,
  2266.    *  the active channel gets unset...
  2267.    */
  2268.   if (type != AUXILLARY_CHANNEL)
  2269.     gimp_image_unset_active_channel (gimage);
  2270. }
  2271.  
  2272. void
  2273. gimp_image_set_component_visible (GimpImage   *gimage, 
  2274.                   ChannelType  type, 
  2275.                   gint         value)
  2276. {
  2277.   /*  No sanity checking here...  */
  2278.   switch (type)
  2279.     {
  2280.     case RED_CHANNEL:     gimage->visible[RED_PIX] = value; break;
  2281.     case GREEN_CHANNEL:   gimage->visible[GREEN_PIX] = value; break;
  2282.     case BLUE_CHANNEL:    gimage->visible[BLUE_PIX] = value; break;
  2283.     case GRAY_CHANNEL:    gimage->visible[GRAY_PIX] = value; break;
  2284.     case INDEXED_CHANNEL: gimage->visible[INDEXED_PIX] = value; break;
  2285.     default: break;
  2286.     }
  2287. }
  2288.  
  2289. Layer *
  2290. gimp_image_pick_correlate_layer (GimpImage *gimage, 
  2291.                  gint       x, 
  2292.                  gint       y)
  2293. {
  2294.   Layer  *layer;
  2295.   GSList *list;
  2296.  
  2297.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2298.  
  2299.   list = gimage->layers;
  2300.   while (list)
  2301.     {
  2302.       layer = (Layer *) list->data;
  2303.       if (layer_pick_correlate (layer, x, y))
  2304.     return layer;
  2305.  
  2306.       list = g_slist_next (list);
  2307.     }
  2308.  
  2309.   return NULL;
  2310. }
  2311.  
  2312.  
  2313.  
  2314. Layer *
  2315. gimp_image_raise_layer (GimpImage *gimage, 
  2316.             Layer     *layer_arg)
  2317. {
  2318.   GSList *list;
  2319.   gint    curpos;
  2320.   
  2321.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2322.   
  2323.   list = gimage->layers;
  2324.   
  2325.   curpos = g_slist_index (list, layer_arg);
  2326.   if (curpos < 0)
  2327.     return NULL;  /* invalid "layer_arg" */
  2328.   
  2329.   /* is this the top layer already? */
  2330.   if (curpos == 0)
  2331.     {
  2332.       g_message (_("Layer cannot be raised any further"));
  2333.       return NULL;
  2334.     }
  2335.   
  2336.   return gimp_image_position_layer (gimage, layer_arg, curpos-1, TRUE);
  2337. }
  2338.  
  2339.  
  2340. Layer *
  2341. gimp_image_lower_layer (GimpImage *gimage, 
  2342.             Layer     *layer_arg)
  2343. {
  2344.   GSList *list;
  2345.   gint    curpos;
  2346.   guint   length;
  2347.   
  2348.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2349.   
  2350.   list = gimage->layers;
  2351.   
  2352.   curpos = g_slist_index (list, layer_arg);
  2353.   if (curpos < 0)
  2354.     return NULL;  /* invalid "layer_arg" */
  2355.   
  2356.   /* is this the bottom layer already? */
  2357.   length = g_slist_length (list);
  2358.   if (curpos == length-1)
  2359.     {
  2360.       g_message (_("Layer cannot be lowered any further"));
  2361.       return NULL;
  2362.     }
  2363.   
  2364.   return gimp_image_position_layer (gimage, layer_arg, curpos+1, TRUE);
  2365. }
  2366.  
  2367.  
  2368.  
  2369. Layer *
  2370. gimp_image_raise_layer_to_top (GimpImage *gimage, 
  2371.                    Layer     *layer_arg)
  2372. {
  2373.   GSList *list;
  2374.   gint    curpos;
  2375.   
  2376.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2377.  
  2378.   list = gimage->layers;
  2379.   
  2380.   curpos = g_slist_index (list, layer_arg);
  2381.   if (curpos < 0)
  2382.     return NULL;
  2383.   
  2384.   if (curpos == 0)
  2385.     {
  2386.       g_message (_("Layer is already on top"));
  2387.       return NULL;
  2388.     }
  2389.   
  2390.   if (! layer_has_alpha (layer_arg))
  2391.     {
  2392.       g_message (_("Can't raise Layer without alpha"));
  2393.       return NULL;
  2394.     }
  2395.   
  2396.   return gimp_image_position_layer (gimage, layer_arg, 0, TRUE);
  2397. }
  2398.  
  2399.  
  2400. Layer *
  2401. gimp_image_lower_layer_to_bottom (GimpImage *gimage, 
  2402.                   Layer     *layer_arg)
  2403. {
  2404.   GSList *list;
  2405.   gint    curpos;
  2406.   guint   length;
  2407.   
  2408.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2409.  
  2410.   list = gimage->layers;
  2411.  
  2412.   curpos = g_slist_index (list, layer_arg);
  2413.   if (curpos < 0)
  2414.     return NULL;
  2415.   
  2416.   length = g_slist_length (list);
  2417.   
  2418.   if (curpos == length-1)
  2419.     {
  2420.       g_message (_("Layer is already on bottom"));
  2421.       return NULL;
  2422.     }
  2423.   
  2424.   return gimp_image_position_layer (gimage, layer_arg, length-1, TRUE);
  2425. }
  2426.  
  2427.  
  2428. Layer *
  2429. gimp_image_position_layer (GimpImage *gimage, 
  2430.                Layer     *layer_arg,
  2431.                gint       new_index,
  2432.                gboolean   push_undo)
  2433. {
  2434.   Layer  *layer;
  2435.   GSList *list;
  2436.   GSList *next;
  2437.   gint    x_min, y_min, x_max, y_max;
  2438.   gint    off_x, off_y;
  2439.   gint    index;
  2440.   gint    list_length;
  2441.  
  2442.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2443.  
  2444.   list = gimage->layers;
  2445.   list_length = g_slist_length (list);
  2446.  
  2447.   next = NULL;
  2448.   layer = NULL;
  2449.   index = 0;
  2450.  
  2451.   /* find layer_arg */
  2452.   index = g_slist_index (list, layer_arg);
  2453.   if (index < 0)
  2454.     {
  2455.       /* The requested layer was not found in the layerstack
  2456.        * Return without changing anything
  2457.        */
  2458.       return NULL;
  2459.     }
  2460.  
  2461.   if (new_index < 0)
  2462.     new_index = 0;
  2463.  
  2464.   if (new_index >= list_length)
  2465.     new_index = list_length - 1;
  2466.  
  2467.   if (new_index == index)
  2468.     return NULL;
  2469.  
  2470.   /* check if we want to move it below a bottom layer without alpha */
  2471.   layer = (Layer *) g_slist_last (list)->data;
  2472.   if (new_index == list_length - 1 &&
  2473.       !layer_has_alpha (layer))
  2474.     {
  2475.       g_message (_("BG has no alpha, layer was placed above"));
  2476.       new_index--;
  2477.     }
  2478.  
  2479.   if (push_undo)
  2480.       undo_push_layer_reposition (gimage, layer_arg);
  2481.  
  2482.   list = g_slist_remove (gimage->layers, layer_arg);
  2483.   gimage->layers = g_slist_insert (list, layer_arg, new_index);
  2484.  
  2485.   /* update the affected area (== area of layer_arg) */
  2486.   drawable_offsets (GIMP_DRAWABLE(layer_arg), &off_x, &off_y);
  2487.   x_min = off_x;
  2488.   y_min = off_y;
  2489.   x_max = off_x + drawable_width (GIMP_DRAWABLE (layer_arg));
  2490.   y_max = off_y + drawable_height (GIMP_DRAWABLE (layer_arg));
  2491.   gtk_signal_emit (GTK_OBJECT (gimage),
  2492.            gimp_image_signals[REPAINT],
  2493.            x_min, y_min, x_max, y_max);
  2494.  
  2495.   /*  invalidate the composite preview  */
  2496.   gimp_image_invalidate_preview (gimage);
  2497.  
  2498.   return layer_arg;
  2499. }
  2500.  
  2501. Layer *
  2502. gimp_image_merge_visible_layers (GimpImage *gimage, 
  2503.                  MergeType  merge_type)
  2504. {
  2505.   GSList   *layer_list;
  2506.   GSList   *merge_list       = NULL;
  2507.   gboolean  had_floating_sel = FALSE;
  2508.   Layer    *layer            = NULL;
  2509.  
  2510.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2511.  
  2512.   /* if there's a floating selection, anchor it */
  2513.   if (gimp_image_floating_sel (gimage))
  2514.     {
  2515.       floating_sel_anchor (gimage->floating_sel);
  2516.       had_floating_sel = TRUE;
  2517.     }
  2518.  
  2519.   layer_list = gimage->layers;
  2520.   while (layer_list)
  2521.     {
  2522.       layer = (Layer *) layer_list->data;
  2523.       if (drawable_visible (GIMP_DRAWABLE(layer)))
  2524.     merge_list = g_slist_append (merge_list, layer);
  2525.  
  2526.       layer_list = g_slist_next (layer_list);
  2527.     }
  2528.  
  2529.   if (merge_list && merge_list->next)
  2530.     {
  2531.       gimp_add_busy_cursors ();
  2532.       layer = gimp_image_merge_layers (gimage, merge_list, merge_type);
  2533.       g_slist_free (merge_list);
  2534.       gimp_remove_busy_cursors (NULL);
  2535.  
  2536.       return layer;
  2537.     }
  2538.   else
  2539.     {
  2540.       g_slist_free (merge_list);
  2541.  
  2542.       /* If there was a floating selection, we have done something.
  2543.      No need to warn the user. Return the active layer instead */
  2544.       if (had_floating_sel)
  2545.     return layer;
  2546.       else
  2547.     g_message (_("There are not enough visible layers for a merge.\nThere must be at least two."));
  2548.  
  2549.       return NULL;
  2550.     }
  2551. }
  2552.  
  2553. Layer *
  2554. gimp_image_flatten (GimpImage *gimage)
  2555. {
  2556.   GSList *layer_list;
  2557.   GSList *merge_list = NULL;
  2558.   Layer  *layer;
  2559.  
  2560.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2561.  
  2562.   gimp_add_busy_cursors ();
  2563.  
  2564.   /* if there's a floating selection, anchor it */
  2565.   if (gimp_image_floating_sel (gimage))
  2566.     floating_sel_anchor (gimage->floating_sel);
  2567.  
  2568.   layer_list = gimage->layers;
  2569.   while (layer_list)
  2570.     {
  2571.       layer = (Layer *) layer_list->data;
  2572.       if (drawable_visible (GIMP_DRAWABLE (layer)))
  2573.     merge_list = g_slist_append (merge_list, layer);
  2574.  
  2575.       layer_list = g_slist_next (layer_list);
  2576.     }
  2577.  
  2578.   layer = gimp_image_merge_layers (gimage, merge_list, FLATTEN_IMAGE);
  2579.   g_slist_free (merge_list);
  2580.  
  2581.   gimp_remove_busy_cursors (NULL);
  2582.  
  2583.   return layer;
  2584. }
  2585.  
  2586. Layer *
  2587. gimp_image_merge_down (GimpImage *gimage,
  2588.                Layer     *current_layer,
  2589.                MergeType  merge_type)
  2590. {
  2591.   GSList *layer_list;
  2592.   GSList *merge_list = NULL;
  2593.   Layer  *layer      = NULL;
  2594.   
  2595.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2596.  
  2597.   layer_list = gimage->layers;
  2598.   while (layer_list)
  2599.     {
  2600.       layer = (Layer *) layer_list->data;
  2601.       if (layer == current_layer)
  2602.     {
  2603.       layer_list = g_slist_next (layer_list);
  2604.       while (layer_list)
  2605.         {
  2606.           layer = (Layer *) layer_list->data;
  2607.           if (drawable_visible (GIMP_DRAWABLE (layer)))
  2608.         {
  2609.           merge_list = g_slist_append (merge_list, layer);
  2610.           layer_list = NULL;
  2611.         }
  2612.           else 
  2613.         layer_list = g_slist_next (layer_list);
  2614.         }
  2615.       merge_list = g_slist_prepend (merge_list, current_layer);
  2616.     }
  2617.       else
  2618.     layer_list = g_slist_next (layer_list);
  2619.     }
  2620.   
  2621.   if (merge_list && merge_list->next)
  2622.     {
  2623.       gimp_add_busy_cursors ();
  2624.       layer = gimp_image_merge_layers (gimage, merge_list, merge_type);
  2625.       g_slist_free (merge_list);
  2626.       gimp_remove_busy_cursors (NULL);
  2627.       return layer;
  2628.     }
  2629.   else 
  2630.     {
  2631.       g_message (_("There are not enough visible layers for a merge down."));
  2632.       g_slist_free (merge_list);
  2633.       return NULL;
  2634.     }
  2635. }
  2636.  
  2637. Layer *
  2638. gimp_image_merge_layers (GimpImage *gimage, 
  2639.              GSList    *merge_list, 
  2640.              MergeType  merge_type)
  2641. {
  2642.   GSList           *reverse_list = NULL;
  2643.   PixelRegion       src1PR, src2PR, maskPR;
  2644.   PixelRegion      *mask;
  2645.   Layer            *merge_layer;
  2646.   Layer            *layer;
  2647.   Layer            *bottom;
  2648.   guchar            bg[4] = {0, 0, 0, 0};
  2649.   GimpImageType     type;
  2650.   gint              count;
  2651.   gint              x1, y1, x2, y2;
  2652.   gint              x3, y3, x4, y4;
  2653.   gint              operation;
  2654.   gint              position;
  2655.   gint              active[MAX_CHANNELS] = {1, 1, 1, 1};
  2656.   gint              off_x, off_y;
  2657.   gchar            *name;
  2658.  
  2659.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  2660.  
  2661.   layer = NULL;
  2662.   type  = RGBA_GIMAGE;
  2663.   x1 = y1 = x2 = y2 = 0;
  2664.   bottom = NULL;
  2665.  
  2666.   /*  Get the layer extents  */
  2667.   count = 0;
  2668.   while (merge_list)
  2669.     {
  2670.       layer = (Layer *) merge_list->data;
  2671.       drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
  2672.  
  2673.       switch (merge_type)
  2674.     {
  2675.     case EXPAND_AS_NECESSARY:
  2676.     case CLIP_TO_IMAGE:
  2677.       if (!count)
  2678.         {
  2679.           x1 = off_x;
  2680.           y1 = off_y;
  2681.           x2 = off_x + drawable_width (GIMP_DRAWABLE(layer));
  2682.           y2 = off_y + drawable_height (GIMP_DRAWABLE(layer));
  2683.         }
  2684.       else
  2685.         {
  2686.           if (off_x < x1)
  2687.         x1 = off_x;
  2688.           if (off_y < y1)
  2689.         y1 = off_y;
  2690.           if ((off_x + drawable_width (GIMP_DRAWABLE(layer))) > x2)
  2691.         x2 = (off_x + drawable_width (GIMP_DRAWABLE(layer)));
  2692.           if ((off_y + drawable_height (GIMP_DRAWABLE(layer))) > y2)
  2693.         y2 = (off_y + drawable_height (GIMP_DRAWABLE(layer)));
  2694.         }
  2695.       if (merge_type == CLIP_TO_IMAGE)
  2696.         {
  2697.           x1 = CLAMP (x1, 0, gimage->width);
  2698.           y1 = CLAMP (y1, 0, gimage->height);
  2699.           x2 = CLAMP (x2, 0, gimage->width);
  2700.           y2 = CLAMP (y2, 0, gimage->height);
  2701.         }
  2702.       break;
  2703.  
  2704.     case CLIP_TO_BOTTOM_LAYER:
  2705.       if (merge_list->next == NULL)
  2706.         {
  2707.           x1 = off_x;
  2708.           y1 = off_y;
  2709.           x2 = off_x + drawable_width (GIMP_DRAWABLE(layer));
  2710.           y2 = off_y + drawable_height (GIMP_DRAWABLE(layer));
  2711.         }
  2712.       break;
  2713.  
  2714.     case FLATTEN_IMAGE:
  2715.       if (merge_list->next == NULL)
  2716.         {
  2717.           x1 = 0;
  2718.           y1 = 0;
  2719.           x2 = gimage->width;
  2720.           y2 = gimage->height;
  2721.         }
  2722.       break;
  2723.     }
  2724.  
  2725.       count ++;
  2726.       reverse_list = g_slist_prepend (reverse_list, layer);
  2727.       merge_list = g_slist_next (merge_list);
  2728.     }
  2729.  
  2730.   if ((x2 - x1) == 0 || (y2 - y1) == 0)
  2731.     return NULL;
  2732.  
  2733.   /* Layer merge listeners tend to be tools performing image map processing */
  2734.   /* and disable the undo system; this signal requests such tools to        */
  2735.   /* perform their cancel actions, releasing the undo system.               */
  2736.  
  2737.   gtk_signal_emit (GTK_OBJECT(gimage), gimp_image_signals[LAYER_MERGE]);
  2738.  
  2739.   /*  Start a merge undo group  */
  2740.  
  2741.   undo_push_group_start (gimage, LAYER_MERGE_UNDO);
  2742.  
  2743.   name = g_strdup (drawable_get_name (GIMP_DRAWABLE(layer)));
  2744.  
  2745.   if (merge_type == FLATTEN_IMAGE ||
  2746.       drawable_type (GIMP_DRAWABLE (layer)) == INDEXED_GIMAGE)
  2747.     {
  2748.       switch (gimp_image_base_type (gimage))
  2749.     {
  2750.     case RGB: type = RGB_GIMAGE; break;
  2751.     case GRAY: type = GRAY_GIMAGE; break;
  2752.     case INDEXED: type = INDEXED_GIMAGE; break;
  2753.     }
  2754.  
  2755.       merge_layer = layer_new (gimage, (x2 - x1), (y2 - y1),
  2756.                    type, drawable_get_name (GIMP_DRAWABLE(layer)),
  2757.                    OPAQUE_OPACITY, NORMAL_MODE);
  2758.       if (!merge_layer)
  2759.     {
  2760.       g_message ("gimp_image_merge_layers: could not allocate merge layer");
  2761.       return NULL;
  2762.     }
  2763.  
  2764.       GIMP_DRAWABLE (merge_layer)->offset_x = x1;
  2765.       GIMP_DRAWABLE (merge_layer)->offset_y = y1;
  2766.  
  2767.       /*  get the background for compositing  */
  2768.       gimp_image_get_background (gimage, GIMP_DRAWABLE (merge_layer), bg);
  2769.  
  2770.       /*  init the pixel region  */
  2771.       pixel_region_init (&src1PR, 
  2772.              drawable_data (GIMP_DRAWABLE (merge_layer)), 
  2773.              0, 0, 
  2774.              gimage->width, gimage->height, 
  2775.              TRUE);
  2776.  
  2777.       /*  set the region to the background color  */
  2778.       color_region (&src1PR, bg);
  2779.  
  2780.       position = 0;
  2781.     }
  2782.   else
  2783.     {
  2784.       /*  The final merged layer inherits the name of the bottom most layer
  2785.        *  and the resulting layer has an alpha channel
  2786.        *  whether or not the original did
  2787.        *  Opacity is set to 100% and the MODE is set to normal
  2788.        */
  2789.  
  2790.       merge_layer = layer_new (gimage, (x2 - x1), (y2 - y1),
  2791.                    drawable_type_with_alpha (GIMP_DRAWABLE(layer)),
  2792.                    "merged layer",
  2793.                    OPAQUE_OPACITY, NORMAL_MODE);
  2794.  
  2795.       if (!merge_layer)
  2796.     {
  2797.       g_message ("gimp_image_merge_layers: could not allocate merge layer");
  2798.       return NULL;
  2799.     }
  2800.  
  2801.       GIMP_DRAWABLE (merge_layer)->offset_x = x1;
  2802.       GIMP_DRAWABLE (merge_layer)->offset_y = y1;
  2803.  
  2804.       /*  Set the layer to transparent  */
  2805.       pixel_region_init (&src1PR, 
  2806.              drawable_data (GIMP_DRAWABLE(merge_layer)), 
  2807.              0, 0, 
  2808.              (x2 - x1), (y2 - y1), 
  2809.              TRUE);
  2810.  
  2811.       /*  set the region to 0's  */
  2812.       color_region (&src1PR, bg);
  2813.  
  2814.       /*  Find the index in the layer list of the bottom layer--we need this
  2815.        *  in order to add the final, merged layer to the layer list correctly
  2816.        */
  2817.       layer = (Layer *) reverse_list->data;
  2818.       position = 
  2819.     g_slist_length (gimage->layers) - 
  2820.     gimp_image_get_layer_index (gimage, layer);
  2821.     }
  2822.  
  2823.   bottom = layer;
  2824.  
  2825.   /* Copy the tattoo and parasites of the bottom layer to the new layer */
  2826.   layer_set_tattoo (merge_layer, layer_get_tattoo (bottom));
  2827.   GIMP_DRAWABLE (merge_layer)->parasites =
  2828.     parasite_list_copy (GIMP_DRAWABLE (bottom)->parasites);
  2829.  
  2830.   while (reverse_list)
  2831.     {
  2832.       LayerModeEffects  mode;
  2833.  
  2834.       layer = (Layer *) reverse_list->data;
  2835.  
  2836.       /*  determine what sort of operation is being attempted and
  2837.        *  if it's actually legal...
  2838.        */
  2839.       operation = valid_combinations [drawable_type (GIMP_DRAWABLE(merge_layer))][drawable_bytes (GIMP_DRAWABLE(layer))];
  2840.       if (operation == -1)
  2841.     {
  2842.       g_message ("gimp_image_merge_layers attempting to merge incompatible layers\n");
  2843.       return NULL;
  2844.     }
  2845.  
  2846.       drawable_offsets (GIMP_DRAWABLE(layer), &off_x, &off_y);
  2847.       x3 = CLAMP (off_x, x1, x2);
  2848.       y3 = CLAMP (off_y, y1, y2);
  2849.       x4 = CLAMP (off_x + drawable_width (GIMP_DRAWABLE(layer)), x1, x2);
  2850.       y4 = CLAMP (off_y + drawable_height (GIMP_DRAWABLE(layer)), y1, y2);
  2851.  
  2852.       /* configure the pixel regions  */
  2853.       pixel_region_init (&src1PR, 
  2854.              drawable_data (GIMP_DRAWABLE(merge_layer)), 
  2855.              (x3 - x1), (y3 - y1), (x4 - x3), (y4 - y3), 
  2856.              TRUE);
  2857.       pixel_region_init (&src2PR, 
  2858.              drawable_data (GIMP_DRAWABLE(layer)), 
  2859.              (x3 - off_x), (y3 - off_y),
  2860.              (x4 - x3), (y4 - y3), 
  2861.              FALSE);
  2862.  
  2863.       if (layer->mask && layer->apply_mask)
  2864.     {
  2865.       pixel_region_init (&maskPR, 
  2866.                  drawable_data (GIMP_DRAWABLE(layer->mask)), 
  2867.                  (x3 - off_x), (y3 - off_y),
  2868.                  (x4 - x3), (y4 - y3), 
  2869.                  FALSE);
  2870.       mask = &maskPR;
  2871.     }
  2872.       else
  2873.     mask = NULL;
  2874.  
  2875.       /* DISSOLVE_MODE is special since it is the only mode that does not
  2876.        *  work on the projection with the lower layer, but only locally on
  2877.        *  the layers alpha channel. 
  2878.        */
  2879.       mode = layer->mode;
  2880.       if (layer == bottom && mode != DISSOLVE_MODE)
  2881.     mode = NORMAL_MODE;
  2882.  
  2883.       combine_regions (&src1PR, &src2PR, &src1PR, mask, NULL,
  2884.                layer->opacity, mode, active, operation);
  2885.  
  2886.       gimp_image_remove_layer (gimage, layer);
  2887.       reverse_list = g_slist_next (reverse_list);
  2888.     }
  2889.  
  2890.   g_slist_free (reverse_list);
  2891.  
  2892.   /*  if the type is flatten, remove all the remaining layers  */
  2893.   if (merge_type == FLATTEN_IMAGE)
  2894.     {
  2895.       merge_list = gimage->layers;
  2896.       while (merge_list)
  2897.     {
  2898.       layer = (Layer *) merge_list->data;
  2899.       merge_list = g_slist_next (merge_list);
  2900.       gimp_image_remove_layer (gimage, layer);
  2901.     }
  2902.  
  2903.       gimp_image_add_layer (gimage, merge_layer, position);
  2904.     }
  2905.   else
  2906.     {
  2907.       /*  Add the layer to the gimage  */
  2908.       gimp_image_add_layer (gimage, merge_layer,
  2909.                 (g_slist_length (gimage->layers) - position + 1));
  2910.     }
  2911.  
  2912.   /* set the name after the original layers have been removed so we don't
  2913.      end up with #2 appended to the name */
  2914.   drawable_set_name (GIMP_DRAWABLE(merge_layer), name);
  2915.   g_free (name);
  2916.  
  2917.   /*  End the merge undo group  */
  2918.   undo_push_group_end (gimage);
  2919.  
  2920.   /*  Update the gimage  */
  2921.   GIMP_DRAWABLE(merge_layer)->visible = TRUE;
  2922.  
  2923.   gtk_signal_emit (GTK_OBJECT(gimage), gimp_image_signals[RESTRUCTURE]);
  2924.  
  2925.   drawable_update (GIMP_DRAWABLE(merge_layer), 
  2926.            0, 0, 
  2927.            drawable_width (GIMP_DRAWABLE(merge_layer)), 
  2928.            drawable_height (GIMP_DRAWABLE(merge_layer)));
  2929.  
  2930.   /*reinit_layer_idlerender (gimage, merge_layer);*/
  2931.  
  2932.   return merge_layer;
  2933. }
  2934.  
  2935. Layer *
  2936. gimp_image_add_layer (GimpImage *gimage, 
  2937.               Layer     *float_layer, 
  2938.               gint       position)
  2939. {
  2940.   LayerUndo *lu;
  2941.   GSList    *ll;
  2942.  
  2943.   if (GIMP_DRAWABLE (float_layer)->gimage != NULL && 
  2944.       GIMP_DRAWABLE (float_layer)->gimage != gimage) 
  2945.     {
  2946.       g_message ("gimp_image_add_layer: attempt to add layer to wrong image");
  2947.       return NULL;
  2948.     }
  2949.  
  2950.   for (ll = gimage->layers; ll; ll = g_slist_next (ll)) 
  2951.     if (ll->data == float_layer) 
  2952.       {
  2953.     g_message ("gimp_image_add_layer: trying to add layer to image twice");
  2954.     return NULL;
  2955.       }
  2956.  
  2957.   /*  Prepare a layer undo and push it  */
  2958.   lu = g_new (LayerUndo, 1);
  2959.   lu->layer         = float_layer;
  2960.   lu->prev_position = 0;
  2961.   lu->prev_layer    = gimage->active_layer;
  2962.   undo_push_layer (gimage, LAYER_ADD_UNDO, lu);
  2963.  
  2964.   /*  If the layer is a floating selection, set the ID  */
  2965.   if (layer_is_floating_sel (float_layer))
  2966.     gimage->floating_sel = float_layer;
  2967.  
  2968.   /*  let the layer know about the gimage  */
  2969.   gimp_drawable_set_gimage (GIMP_DRAWABLE (float_layer), gimage);
  2970.   
  2971.   /*  If the layer has a mask, set the mask's gimage and layer */
  2972.   if (float_layer->mask)
  2973.     {
  2974.       gimp_drawable_set_gimage (GIMP_DRAWABLE (float_layer->mask), gimage);
  2975.     }
  2976.  
  2977.   /*  add the layer to the list at the specified position  */
  2978.   if (position == -1)
  2979.     position = gimp_image_get_layer_index (gimage, gimage->active_layer);
  2980.  
  2981.   if (position != -1)
  2982.     {
  2983.       /*  If there is a floating selection (and this isn't it!),
  2984.        *  make sure the insert position is greater than 0
  2985.        */
  2986.       if (gimp_image_floating_sel (gimage) &&
  2987.       (gimage->floating_sel != float_layer) && position == 0)
  2988.     position = 1;
  2989.  
  2990.       gimage->layers = 
  2991.     g_slist_insert (gimage->layers, layer_ref (float_layer), position);
  2992.     }
  2993.   else
  2994.     {
  2995.       gimage->layers = 
  2996.     g_slist_prepend (gimage->layers, layer_ref (float_layer));
  2997.     }
  2998.  
  2999.   gimage->layer_stack = g_slist_prepend (gimage->layer_stack, float_layer);
  3000.  
  3001.   /*  notify the layers dialog of the currently active layer  */
  3002.   gimp_image_set_active_layer (gimage, float_layer);
  3003.  
  3004.   /*  update the new layer's area  */
  3005.   drawable_update (GIMP_DRAWABLE (float_layer),
  3006.            0, 0,
  3007.            drawable_width  (GIMP_DRAWABLE (float_layer)),
  3008.            drawable_height (GIMP_DRAWABLE (float_layer)));
  3009.  
  3010.   /*  invalidate the composite preview  */
  3011.   gimp_image_invalidate_preview (gimage);
  3012.  
  3013.   return float_layer;
  3014. }
  3015.  
  3016. Layer *
  3017. gimp_image_remove_layer (GimpImage *gimage, 
  3018.              Layer     *layer)
  3019. {
  3020.   LayerUndo *lu;
  3021.  
  3022.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3023.  
  3024.   if (layer)
  3025.     {
  3026.       /*  Prepare a layer undo--push it at the end  */
  3027.       lu = g_new (LayerUndo, 1);
  3028.       lu->layer         = layer;
  3029.       lu->prev_position = gimp_image_get_layer_index (gimage, layer);
  3030.       lu->prev_layer    = layer;
  3031.  
  3032.       gimage->layers = g_slist_remove (gimage->layers, layer);
  3033.       gimage->layer_stack = g_slist_remove (gimage->layer_stack, layer);
  3034.  
  3035.       /*  If this was the floating selection, reset the fs pointer  */
  3036.       if (gimage->floating_sel == layer)
  3037.     {
  3038.       gimage->floating_sel = NULL;
  3039.  
  3040.       floating_sel_reset (layer);
  3041.     }
  3042.       if (gimage->active_layer == layer)
  3043.     {
  3044.       if (gimage->layers)
  3045.         gimage_set_active_layer (gimage, gimage->layer_stack->data);
  3046.       else
  3047.         gimage->active_layer = NULL;
  3048.     }
  3049.  
  3050.       /* Send out REMOVED signal from layer */
  3051.       layer_removed (layer, gimage);
  3052.  
  3053.       /*  Push the layer undo--It is important it goes here since layer might
  3054.        *   be immediately destroyed if the undo push fails
  3055.        */
  3056.       undo_push_layer (gimage, LAYER_REMOVE_UNDO, lu);
  3057.  
  3058.       /*  invalidate the composite preview  */
  3059.       gimp_image_invalidate_preview (gimage);
  3060.       gdisplays_update_full(gimage);
  3061.  
  3062.       return NULL;
  3063.     }
  3064.   else
  3065.     return NULL;
  3066. }
  3067.  
  3068. LayerMask *
  3069. gimp_image_add_layer_mask (GimpImage *gimage, 
  3070.                Layer     *layer, 
  3071.                LayerMask *mask)
  3072. {
  3073.   LayerMaskUndo *lmu;
  3074.  
  3075.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3076.  
  3077.   if (layer->mask != NULL)
  3078.     {
  3079.       g_message(_("Unable to add a layer mask since\nthe layer already has one."));
  3080.       return (NULL);
  3081.     }
  3082.  
  3083.   if (drawable_indexed (GIMP_DRAWABLE (layer)))
  3084.     {
  3085.       g_message(_("Unable to add a layer mask to a\nlayer in an indexed image."));
  3086.       return (NULL);
  3087.     }
  3088.  
  3089.   if (! layer_has_alpha (layer))
  3090.     {
  3091.       g_message (_("Cannot add layer mask to a layer\nwith no alpha channel."));
  3092.       return (NULL);
  3093.     }
  3094.  
  3095.   if ((drawable_width (GIMP_DRAWABLE(layer)) !=
  3096.        drawable_width (GIMP_DRAWABLE(mask))) ||
  3097.       (drawable_height (GIMP_DRAWABLE(layer)) !=
  3098.        drawable_height (GIMP_DRAWABLE(mask))))
  3099.     {
  3100.       g_message(_("Cannot add layer mask of different dimensions than specified layer."));
  3101.       return NULL;
  3102.     }
  3103.  
  3104.   layer_add_mask (layer, mask);
  3105.  
  3106.   /*  Prepare a layer undo and push it  */
  3107.   lmu = g_new (LayerMaskUndo, 1);
  3108.   lmu->layer      = layer;
  3109.   lmu->mask       = mask;
  3110.   lmu->apply_mask = layer->apply_mask;
  3111.   lmu->edit_mask  = layer->edit_mask;
  3112.   lmu->show_mask  = layer->show_mask;
  3113.   undo_push_layer_mask (gimage, LAYER_MASK_ADD_UNDO, lmu);
  3114.   
  3115.   return mask;
  3116. }
  3117.  
  3118. Channel *
  3119. gimp_image_remove_layer_mask (GimpImage     *gimage, 
  3120.                   Layer         *layer, 
  3121.                   MaskApplyMode  mode)
  3122. {
  3123.   LayerMaskUndo *lmu;
  3124.   gint           off_x;
  3125.   gint           off_y;
  3126.  
  3127.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage) && GIMP_IS_LAYER (layer), NULL);
  3128.  
  3129.   if (!layer || !layer->mask)
  3130.     return NULL;
  3131.  
  3132.   /*  Start an undo group  */
  3133.   undo_push_group_start (gimage, LAYER_APPLY_MASK_UNDO);
  3134.  
  3135.   /*  Prepare a layer mask undo--push it below  */
  3136.   lmu = g_new (LayerMaskUndo, 1);
  3137.   lmu->layer      = layer;
  3138.   lmu->mask       = layer->mask;
  3139.   lmu->mode       = mode;
  3140.   lmu->apply_mask = layer->apply_mask;
  3141.   lmu->edit_mask  = layer->edit_mask;
  3142.   lmu->show_mask  = layer->show_mask;
  3143.  
  3144.   layer_apply_mask (layer, mode);
  3145.  
  3146.   /*  Push the undo--Important to do it here, AFTER the call
  3147.    *   to layer_apply_mask, in case the undo push fails and the
  3148.    *   mask is deleted
  3149.    */
  3150.   undo_push_layer_mask (gimage, LAYER_MASK_REMOVE_UNDO, lmu);
  3151.  
  3152.   /*  end the undo group  */
  3153.   undo_push_group_end (gimage);
  3154.  
  3155.   /*  If applying actually changed the view  */
  3156.   if ((mode == APPLY   && (!lmu->apply_mask || lmu->show_mask)) ||
  3157.       (mode == DISCARD && ( lmu->apply_mask || lmu->show_mask)))
  3158.     {
  3159.       gimp_image_invalidate_preview (gimage);
  3160.  
  3161.       drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
  3162.       gtk_signal_emit (GTK_OBJECT (gimage),
  3163.                gimp_image_signals[REPAINT],
  3164.                off_x, off_y,
  3165.                drawable_width  (GIMP_DRAWABLE (layer)),
  3166.                drawable_height (GIMP_DRAWABLE (layer)));
  3167.     }
  3168.   else if (lmu->edit_mask)
  3169.     {
  3170.       /*  flush displays in order to update the menus  */
  3171.       gdisplays_flush ();
  3172.     }
  3173.  
  3174.   return NULL;
  3175. }
  3176.  
  3177. Channel *
  3178. gimp_image_raise_channel (GimpImage *gimage, 
  3179.               Channel   *channel_arg)
  3180. {
  3181.   Channel *channel;
  3182.   Channel *prev_channel;
  3183.   GSList  *list;
  3184.   GSList  *prev;
  3185.   gint     index = -1;
  3186.  
  3187.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3188.  
  3189.   list = gimage->channels;
  3190.   prev = NULL;
  3191.   prev_channel = NULL;
  3192.  
  3193.   while (list)
  3194.     {
  3195.       channel = (Channel *) list->data;
  3196.       if (prev)
  3197.     prev_channel = (Channel *) prev->data;
  3198.  
  3199.       if (channel == channel_arg)
  3200.     {
  3201.       if (prev)
  3202.         {
  3203.           list->data = prev_channel;
  3204.           prev->data = channel;
  3205.           drawable_update (GIMP_DRAWABLE (channel),
  3206.                    0, 0,
  3207.                    drawable_width  (GIMP_DRAWABLE (channel)),
  3208.                    drawable_height (GIMP_DRAWABLE (channel)));
  3209.           return prev_channel;
  3210.         }
  3211.       else
  3212.         {
  3213.           g_message (_("Channel cannot be raised any further"));
  3214.           return NULL;
  3215.         }
  3216.     }
  3217.  
  3218.       prev = list;
  3219.       index++;
  3220.       list = g_slist_next (list);
  3221.     }
  3222.  
  3223.   return NULL;
  3224. }
  3225.  
  3226. Channel *
  3227. gimp_image_lower_channel (GimpImage *gimage, 
  3228.               Channel   *channel_arg)
  3229. {
  3230.   Channel *channel;
  3231.   Channel *next_channel;
  3232.   GSList  *list;
  3233.   GSList  *next;
  3234.   gint     index = 0;
  3235.  
  3236.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3237.  
  3238.   list = gimage->channels;
  3239.   next_channel = NULL;
  3240.  
  3241.   while (list)
  3242.     {
  3243.       channel = (Channel *) list->data;
  3244.       next = g_slist_next (list);
  3245.  
  3246.       if (next)
  3247.     next_channel = (Channel *) next->data;
  3248.       index++;
  3249.  
  3250.       if (channel == channel_arg)
  3251.     {
  3252.       if (next)
  3253.         {
  3254.           list->data = next_channel;
  3255.           next->data = channel;
  3256.           drawable_update (GIMP_DRAWABLE (channel),
  3257.                    0, 0,
  3258.                    drawable_width  (GIMP_DRAWABLE (channel)),
  3259.                    drawable_height (GIMP_DRAWABLE (channel)));
  3260.           return next_channel;
  3261.         }
  3262.       else
  3263.         {
  3264.           g_message (_("Channel cannot be lowered any further"));
  3265.           return NULL;
  3266.         }
  3267.     }
  3268.  
  3269.       list = next;
  3270.     }
  3271.  
  3272.   return NULL;
  3273. }
  3274.  
  3275. Channel *
  3276. gimp_image_position_channel (GimpImage *gimage, 
  3277.                  Channel   *channel_arg,
  3278.                  gint       new_index)
  3279. {
  3280.   Channel *channel;
  3281.   GSList  *list;
  3282.   GSList  *next;
  3283.   gint     index;
  3284.   gint     list_length;
  3285.  
  3286.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3287.  
  3288.   next    = NULL;
  3289.   channel = NULL;
  3290.  
  3291.   /* find channel_arg */
  3292.   for (list = gimage->channels, index = 0, list_length = g_slist_length (list);
  3293.        list;
  3294.        list = g_slist_next (list), index++)
  3295.     {
  3296.       channel = (Channel *) list->data;
  3297.       if (channel == channel_arg)
  3298.     {
  3299.       break;
  3300.     }
  3301.     }
  3302.  
  3303.   if (channel != channel_arg)
  3304.     {
  3305.       /* The requested channel was not found in the channel stack
  3306.        * Return without changing anything
  3307.        */
  3308.       return NULL;
  3309.     }
  3310.  
  3311.   if (new_index < 0)
  3312.     new_index = 0;
  3313.  
  3314.   if (new_index >= list_length)
  3315.     new_index = list_length - 1;
  3316.  
  3317.   list = g_slist_remove (gimage->channels, channel);
  3318.   gimage->channels = g_slist_insert (list, channel, new_index);
  3319.  
  3320.   drawable_update (GIMP_DRAWABLE (channel),
  3321.            0, 0,
  3322.            drawable_width  (GIMP_DRAWABLE (channel)),
  3323.            drawable_height (GIMP_DRAWABLE (channel)));
  3324.  
  3325.   return channel;
  3326. }
  3327.  
  3328. Channel *
  3329. gimp_image_add_channel (GimpImage *gimage, 
  3330.             Channel   *channel, 
  3331.             gint       position)
  3332. {
  3333.   ChannelUndo *cu;
  3334.   GSList      *cc;
  3335.  
  3336.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage) && 
  3337.             GIMP_IS_CHANNEL (channel), NULL);
  3338.  
  3339.   if (GIMP_DRAWABLE(channel)->gimage != NULL &&
  3340.       GIMP_DRAWABLE(channel)->gimage != gimage)
  3341.     {
  3342.       g_message ("gimp_image_add_channel: attempt to add channel to wrong image");
  3343.       return NULL;
  3344.     }
  3345.  
  3346.   for (cc = gimage->channels; cc; cc = g_slist_next (cc))
  3347.     if (cc->data == channel) 
  3348.       {
  3349.     g_message ("gimp_image_add_channel: trying to add channel to image twice");
  3350.     return NULL;
  3351.       }
  3352.  
  3353.   /*  Prepare a channel undo and push it  */
  3354.   cu = g_new (ChannelUndo, 1);
  3355.   cu->channel       = channel;
  3356.   cu->prev_position = 0;
  3357.   cu->prev_channel  = gimage->active_channel;
  3358.   undo_push_channel (gimage, CHANNEL_ADD_UNDO, cu);
  3359.  
  3360.   /*  add the channel to the list  */
  3361.   gimage->channels = g_slist_prepend (gimage->channels, channel_ref (channel));
  3362.  
  3363.   /*  notify this gimage of the currently active channel  */
  3364.   gimp_image_set_active_channel (gimage, channel);
  3365.  
  3366.   /*  if channel is visible, update the image  */
  3367.   if (drawable_visible (GIMP_DRAWABLE(channel)))
  3368.     drawable_update (GIMP_DRAWABLE(channel), 
  3369.              0, 0, 
  3370.              drawable_width (GIMP_DRAWABLE(channel)), 
  3371.              drawable_height (GIMP_DRAWABLE(channel)));
  3372.  
  3373.   return channel;
  3374. }
  3375.  
  3376. Channel *
  3377. gimp_image_remove_channel (GimpImage *gimage, 
  3378.                Channel   *channel)
  3379. {
  3380.   ChannelUndo *cu;
  3381.  
  3382.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage) && 
  3383.             GIMP_IS_CHANNEL (channel), NULL);
  3384.  
  3385.   /*  Prepare a channel undo--push it below  */
  3386.   cu = g_new (ChannelUndo, 1);
  3387.   cu->channel       = channel;
  3388.   cu->prev_position = gimp_image_get_channel_index (gimage, channel);
  3389.   cu->prev_channel  = gimage->active_channel;
  3390.   
  3391.   gimage->channels = g_slist_remove (gimage->channels, channel);
  3392.   
  3393.   if (gimage->active_channel == channel)
  3394.     {
  3395.       if (gimage->channels)
  3396.     gimage->active_channel = (((Channel *) gimage->channels->data));
  3397.       else
  3398.     gimage->active_channel = NULL;
  3399.     }
  3400.   
  3401.   if (drawable_visible (GIMP_DRAWABLE(channel)))
  3402.     drawable_update (GIMP_DRAWABLE(channel), 
  3403.              0, 0, 
  3404.              drawable_width (GIMP_DRAWABLE(channel)), 
  3405.              drawable_height (GIMP_DRAWABLE(channel)));
  3406.   
  3407.   /* Send out REMOVED signal from channel */
  3408.   channel_removed (channel, gimage);
  3409.   
  3410.   /*  Important to push the undo here in case the push fails  */
  3411.   undo_push_channel (gimage, CHANNEL_REMOVE_UNDO, cu);
  3412.   
  3413.   return channel;
  3414. }
  3415.  
  3416. /************************************************************/
  3417. /*  Access functions                                        */
  3418. /************************************************************/
  3419.  
  3420. gboolean
  3421. gimp_image_is_empty (GimpImage *gimage)
  3422. {
  3423.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), TRUE);
  3424.  
  3425.   return (! gimage->layers);
  3426. }
  3427.  
  3428. GimpDrawable *
  3429. gimp_image_active_drawable (GimpImage *gimage)
  3430. {
  3431.   Layer *layer;
  3432.  
  3433.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3434.  
  3435.   /*  If there is an active channel (a saved selection, etc.),
  3436.    *  we ignore the active layer
  3437.    */
  3438.   if (gimage->active_channel != NULL)
  3439.     return GIMP_DRAWABLE (gimage->active_channel);
  3440.   else if (gimage->active_layer != NULL)
  3441.     {
  3442.       layer = gimage->active_layer;
  3443.       if (layer->mask && layer->edit_mask)
  3444.     return GIMP_DRAWABLE(layer->mask);
  3445.       else
  3446.     return GIMP_DRAWABLE(layer);
  3447.     }
  3448.   else
  3449.     return NULL;
  3450. }
  3451.  
  3452. GimpImageBaseType
  3453. gimp_image_base_type (const GimpImage *gimage)
  3454. {  
  3455.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  3456.  
  3457.   return gimage->base_type;
  3458. }
  3459.  
  3460. GimpImageType
  3461. gimp_image_base_type_with_alpha (const GimpImage *gimage)
  3462. {
  3463.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  3464.  
  3465.   switch (gimage->base_type)
  3466.     {
  3467.     case RGB:
  3468.       return RGBA_GIMAGE;
  3469.     case GRAY:
  3470.       return GRAYA_GIMAGE;
  3471.     case INDEXED:
  3472.       return INDEXEDA_GIMAGE;
  3473.     }
  3474.   return RGB_GIMAGE;
  3475. }
  3476.  
  3477. #ifdef GDK_USE_UTF8_MBS
  3478. /* Note: Returns UTF-8 */
  3479. #endif
  3480.  
  3481. gchar *
  3482. gimp_image_filename (GimpImage *gimage)
  3483. {
  3484.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3485.  
  3486.   if (gimage->has_filename)
  3487. #ifdef GDK_USE_UTF8_MBS
  3488.     /* Yes, this leaks memory. So shoot me. The back-and-forth
  3489.      * UTF-8 conversions are a mess anyway. Sigh.
  3490.      */
  3491.     return g_filename_to_utf8 (gimage->filename, -1, NULL, NULL, NULL);
  3492. #else
  3493.     return gimage->filename;
  3494. #endif
  3495.   else
  3496.     return _("Untitled");
  3497. }
  3498.  
  3499. gboolean
  3500. gimp_image_undo_is_enabled (GimpImage *gimage)
  3501. {
  3502.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3503.  
  3504.   return gimage->undo_on;
  3505. }
  3506.  
  3507. gboolean
  3508. gimp_image_undo_freeze (GimpImage *gimage)
  3509. {
  3510.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3511.  
  3512.   gimage->undo_on = FALSE;
  3513.  
  3514.   return TRUE;
  3515. }
  3516.  
  3517. gboolean
  3518. gimp_image_undo_thaw (GimpImage *gimage)
  3519. {
  3520.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3521.  
  3522.   gimage->undo_on = TRUE;
  3523.  
  3524.   return TRUE;
  3525. }
  3526.  
  3527. gboolean
  3528. gimp_image_undo_disable (GimpImage *gimage)
  3529. {
  3530.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3531.  
  3532.   return gimp_image_undo_freeze (gimage);
  3533. }
  3534.  
  3535. gboolean
  3536. gimp_image_undo_enable (GimpImage *gimage)
  3537. {
  3538.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3539.  
  3540.   /*  Free all undo steps as they are now invalidated  */
  3541.   undo_free (gimage);
  3542.  
  3543.   return gimp_image_undo_thaw (gimage);
  3544. }
  3545.  
  3546. void
  3547. gimp_image_undo_event (GimpImage *gimage, 
  3548.                int        event)
  3549. {
  3550.   gtk_signal_emit (GTK_OBJECT (gimage), gimp_image_signals[UNDO_EVENT], event);
  3551. }
  3552.  
  3553.  
  3554. /* NOTE about the gimage->dirty counter:
  3555.  *   If 0, then the image is clean (ie, copy on disk is the same as the one 
  3556.  *      in memory).
  3557.  *   If positive, then that's the number of dirtying operations done
  3558.  *       on the image since the last save.
  3559.  *   If negative, then user has hit undo and gone back in time prior
  3560.  *       to the saved copy.  Hitting redo will eventually come back to
  3561.  *       the saved copy.
  3562.  *
  3563.  *   The image is dirty (ie, needs saving) if counter is non-zero.
  3564.  *
  3565.  *   If the counter is around 10000, this is due to undo-ing back
  3566.  *   before a saved version, then mutating the image (thus destroying
  3567.  *   the redo stack).  Once this has happened, it's impossible to get
  3568.  *   the image back to the state on disk, since the redo info has been
  3569.  *   freed.  See undo.c for the gorey details.
  3570.  */
  3571.  
  3572.  
  3573. /*
  3574.  * NEVER CALL gimp_image_dirty() directly!
  3575.  *
  3576.  * If your code has just dirtied the image, push an undo instead.
  3577.  * Failing that, push the trivial undo which tells the user the
  3578.  * command is not undoable: undo_push_cantundo() (But really, it would
  3579.  * be best to push a proper undo).  If you just dirty the image
  3580.  * without pushing an undo then the dirty count is increased, but
  3581.  * popping that many undo actions won't lead to a clean image.
  3582.  */
  3583.  
  3584. gint
  3585. gimp_image_dirty (GimpImage *gimage)
  3586. {
  3587.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3588.  
  3589.   gimage->dirty++;
  3590.   gtk_signal_emit(GTK_OBJECT(gimage), gimp_image_signals[DIRTY]);
  3591.   
  3592.   TRC (("dirty %d -> %d\n", gimage->dirty-1, gimage->dirty));
  3593.   
  3594.   return gimage->dirty;
  3595. }
  3596.  
  3597. gint
  3598. gimp_image_clean (GimpImage *gimage)
  3599. {
  3600.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3601.  
  3602.   gimage->dirty--;
  3603.   gtk_signal_emit(GTK_OBJECT(gimage), gimp_image_signals[CLEAN]);
  3604.   
  3605.   TRC (("clean %d -> %d\n", gimage->dirty+1, gimage->dirty));
  3606.   
  3607.   return gimage->dirty;
  3608. }
  3609.  
  3610. void
  3611. gimp_image_clean_all (GimpImage *gimage)
  3612. {
  3613.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  3614.  
  3615.   gimage->dirty = 0;
  3616.  
  3617.   gtk_signal_emit (GTK_OBJECT(gimage), gimp_image_signals[CLEAN]);
  3618. }
  3619.  
  3620. Layer *
  3621. gimp_image_floating_sel (GimpImage *gimage)
  3622. {
  3623.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), FALSE);
  3624.  
  3625.   if (gimage->floating_sel == NULL)
  3626.     return NULL;
  3627.   else
  3628.     return gimage->floating_sel;
  3629. }
  3630.  
  3631. guchar *
  3632. gimp_image_cmap (GimpImage *gimage)
  3633. {
  3634.   return drawable_cmap (gimp_image_active_drawable (gimage));
  3635. }
  3636.  
  3637. /************************************************************/
  3638. /*  Projection access functions                             */
  3639. /************************************************************/
  3640.  
  3641. TileManager *
  3642. gimp_image_projection (GimpImage *gimage)
  3643. {
  3644.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3645.  
  3646.   if ((gimage->projection == NULL) ||
  3647.       (tile_manager_level_width (gimage->projection) != gimage->width) ||
  3648.       (tile_manager_level_height (gimage->projection) != gimage->height))
  3649.     gimp_image_allocate_projection (gimage);
  3650.   
  3651.   return gimage->projection;
  3652. }
  3653.  
  3654. GimpImageType
  3655. gimp_image_projection_type (const GimpImage *gimage)
  3656. {
  3657.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  3658.  
  3659.   return gimage->proj_type;
  3660. }
  3661.  
  3662. gint
  3663. gimp_image_projection_bytes (const GimpImage *gimage)
  3664. {
  3665.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  3666.  
  3667.   return gimage->proj_bytes;
  3668. }
  3669.  
  3670. gint
  3671. gimp_image_projection_opacity (const GimpImage *gimage)
  3672. {
  3673.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), -1);
  3674.  
  3675.   return OPAQUE_OPACITY;
  3676. }
  3677.  
  3678. void
  3679. gimp_image_projection_realloc (GimpImage *gimage)
  3680. {
  3681.   gimp_image_allocate_projection (gimage);
  3682. }
  3683.  
  3684. /************************************************************/
  3685. /*  Composition access functions                            */
  3686. /************************************************************/
  3687.  
  3688. TileManager *
  3689. gimp_image_composite (GimpImage *gimage)
  3690. {
  3691.   return gimp_image_projection (gimage);
  3692. }
  3693.  
  3694. GimpImageType
  3695. gimp_image_composite_type (const GimpImage *gimage)
  3696. {
  3697.   return gimp_image_projection_type (gimage);
  3698. }
  3699.  
  3700. gint
  3701. gimp_image_composite_bytes (const GimpImage *gimage)
  3702. {
  3703.   return gimp_image_projection_bytes (gimage);
  3704. }
  3705.  
  3706. TempBuf *
  3707. gimp_image_construct_composite_preview (GimpImage *gimage, 
  3708.                     gint       width, 
  3709.                     gint       height)
  3710. {
  3711.   Layer       *layer;
  3712.   Layer       *floating_sel;
  3713.   PixelRegion  src1PR, src2PR, maskPR;
  3714.   PixelRegion *mask;
  3715.   TempBuf     *comp;
  3716.   TempBuf     *layer_buf;
  3717.   TempBuf     *mask_buf;
  3718.   GSList      *list;
  3719.   GSList      *reverse_list = NULL;
  3720.   gdouble      ratio;
  3721.   gint         x, y, w, h;
  3722.   gint         x1, y1, x2, y2;
  3723.   gint         bytes;
  3724.   gint         construct_flag;
  3725.   gint         visible[MAX_CHANNELS] = {1, 1, 1, 1};
  3726.   gint         off_x, off_y;
  3727.  
  3728.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3729.  
  3730.   ratio = (gdouble) width / (gdouble) gimage->width;
  3731.  
  3732.   switch (gimp_image_base_type (gimage))
  3733.     {
  3734.     case RGB:
  3735.     case INDEXED:
  3736.       bytes = 4;
  3737.       break;
  3738.     case GRAY:
  3739.       bytes = 2;
  3740.       break;
  3741.     default:
  3742.       bytes = 0;
  3743.       break;
  3744.     }
  3745.  
  3746.   /*  The construction buffer  */
  3747.   comp = temp_buf_new (width, height, bytes, 0, 0, NULL);
  3748.   memset (temp_buf_data (comp), 0, comp->width * comp->height * comp->bytes);
  3749.  
  3750.   floating_sel = NULL;
  3751.   for (list = gimage->layers; list; list = g_slist_next (list))
  3752.     {
  3753.       layer = (Layer *) list->data;
  3754.  
  3755.       /*  only add layers that are visible to the list  */
  3756.       if (drawable_visible (GIMP_DRAWABLE (layer)))
  3757.     {
  3758.       /*  floating selections are added right above the layer 
  3759.           they are attached to  */
  3760.       if (layer_is_floating_sel (layer))
  3761.         floating_sel = layer;
  3762.       else
  3763.         {
  3764.           if (floating_sel && 
  3765.           floating_sel->fs.drawable == GIMP_DRAWABLE (layer))
  3766.         reverse_list = g_slist_prepend (reverse_list, floating_sel);
  3767.  
  3768.           reverse_list = g_slist_prepend (reverse_list, layer);
  3769.         }
  3770.     }
  3771.     }
  3772.  
  3773.   construct_flag = 0;
  3774.  
  3775.   while (reverse_list)
  3776.     {
  3777.       layer = (Layer *) reverse_list->data;
  3778.  
  3779.       drawable_offsets (GIMP_DRAWABLE (layer), &off_x, &off_y);
  3780.  
  3781.       x = (gint) RINT (ratio * off_x);
  3782.       y = (gint) RINT (ratio * off_y); 
  3783.       w = (gint) RINT (ratio * drawable_width (GIMP_DRAWABLE (layer))); 
  3784.       h = (gint) RINT (ratio * drawable_height (GIMP_DRAWABLE (layer))); 
  3785.       
  3786.       x1 = CLAMP (x, 0, width);
  3787.       y1 = CLAMP (y, 0, height);
  3788.       x2 = CLAMP (x + w, 0, width);
  3789.       y2 = CLAMP (y + h, 0, height);
  3790.  
  3791.       src1PR.bytes     = comp->bytes;
  3792.       src1PR.x         = x1;
  3793.       src1PR.y         = y1;
  3794.       src1PR.w         = (x2 - x1);
  3795.       src1PR.h         = (y2 - y1);
  3796.       src1PR.rowstride = comp->width * src1PR.bytes;
  3797.       src1PR.data      = 
  3798.     temp_buf_data (comp) + y1 * src1PR.rowstride + x1 * src1PR.bytes;
  3799.  
  3800.       layer_buf = layer_preview (layer, w, h);
  3801.       src2PR.bytes     = layer_buf->bytes;
  3802.       src2PR.w         = src1PR.w;  
  3803.       src2PR.h         = src1PR.h;
  3804.       src2PR.x         = src1PR.x; 
  3805.       src2PR.y         = src1PR.y;
  3806.       src2PR.rowstride = layer_buf->width * src2PR.bytes;
  3807.       src2PR.data      = temp_buf_data (layer_buf) + 
  3808.     (y1 - y) * src2PR.rowstride + (x1 - x) * src2PR.bytes;
  3809.  
  3810.       if (layer->mask && layer->apply_mask)
  3811.     {
  3812.       mask_buf = layer_mask_preview (layer, w, h);
  3813.       maskPR.bytes     = mask_buf->bytes;
  3814.       maskPR.rowstride = mask_buf->width;
  3815.       maskPR.data      = mask_buf_data (mask_buf) +
  3816.         (y1 - y) * maskPR.rowstride + (x1 - x) * maskPR.bytes;
  3817.       mask = &maskPR;
  3818.     }
  3819.       else
  3820.     mask = NULL;
  3821.  
  3822.       /*  Based on the type of the layer, project the layer onto the
  3823.        *   composite preview...
  3824.        *  Indexed images are actually already converted to RGB and RGBA,
  3825.        *   so just project them as if they were type "intensity"
  3826.        *  Send in all TRUE for visible since that info doesn't matter
  3827.        *   for previews
  3828.        */
  3829.       switch (drawable_type (GIMP_DRAWABLE(layer)))
  3830.     {
  3831.     case RGB_GIMAGE: case GRAY_GIMAGE: case INDEXED_GIMAGE:
  3832.       if (! construct_flag)
  3833.         initial_region (&src2PR, &src1PR, 
  3834.                 mask, NULL, layer->opacity,
  3835.                 layer->mode, visible, INITIAL_INTENSITY);
  3836.       else
  3837.         combine_regions (&src1PR, &src2PR, &src1PR, 
  3838.                  mask, NULL, layer->opacity,
  3839.                  layer->mode, visible, COMBINE_INTEN_A_INTEN);
  3840.       break;
  3841.  
  3842.     case RGBA_GIMAGE: case GRAYA_GIMAGE: case INDEXEDA_GIMAGE:
  3843.       if (! construct_flag)
  3844.         initial_region (&src2PR, &src1PR, 
  3845.                 mask, NULL, layer->opacity,
  3846.                 layer->mode, visible, INITIAL_INTENSITY_ALPHA);
  3847.       else
  3848.         combine_regions (&src1PR, &src2PR, &src1PR, 
  3849.                  mask, NULL, layer->opacity,
  3850.                  layer->mode, visible, COMBINE_INTEN_A_INTEN_A);
  3851.       break;
  3852.  
  3853.     default:
  3854.       break;
  3855.     }
  3856.  
  3857.       construct_flag = 1;
  3858.  
  3859.       reverse_list = g_slist_next (reverse_list);
  3860.     }
  3861.  
  3862.   g_slist_free (reverse_list);
  3863.  
  3864.   return comp;
  3865. }
  3866.  
  3867. TempBuf *
  3868. gimp_image_composite_preview (GimpImage   *gimage, 
  3869.                   ChannelType  type,
  3870.                   gint         width, 
  3871.                   gint         height)
  3872. {
  3873.   gint channel;
  3874.  
  3875.   switch (type)
  3876.     {
  3877.     case RED_CHANNEL:     channel = RED_PIX; break;
  3878.     case GREEN_CHANNEL:   channel = GREEN_PIX; break;
  3879.     case BLUE_CHANNEL:    channel = BLUE_PIX; break;
  3880.     case GRAY_CHANNEL:    channel = GRAY_PIX; break;
  3881.     case INDEXED_CHANNEL: channel = INDEXED_PIX; break;
  3882.     default: return NULL;
  3883.     }
  3884.  
  3885.   g_return_val_if_fail (GIMP_IS_IMAGE (gimage), NULL);
  3886.  
  3887.   /*  The easy way  */
  3888.   if (gimage->comp_preview_valid[channel] &&
  3889.       gimage->comp_preview->width == width &&
  3890.       gimage->comp_preview->height == height)
  3891.     return gimage->comp_preview;
  3892.   /*  The hard way  */
  3893.   else
  3894.     {
  3895.       if (gimage->comp_preview)
  3896.     temp_buf_free (gimage->comp_preview);
  3897.  
  3898.       /*  Actually construct the composite preview from the layer previews!
  3899.        *  This might seem ridiculous, but it's actually the best way, given
  3900.        *  a number of unsavory alternatives.
  3901.        */
  3902.       gimage->comp_preview = 
  3903.     gimp_image_construct_composite_preview (gimage, width, height);
  3904.       gimage->comp_preview_valid[channel] = TRUE;
  3905.  
  3906.       return gimage->comp_preview;
  3907.     }
  3908. }
  3909.  
  3910. gboolean
  3911. gimp_image_preview_valid (GimpImage   *gimage, 
  3912.               ChannelType  type)
  3913. {
  3914.   switch (type)
  3915.     {
  3916.     case RED_CHANNEL:     return gimage->comp_preview_valid[RED_PIX];     break;
  3917.     case GREEN_CHANNEL:   return gimage->comp_preview_valid[GREEN_PIX];   break;
  3918.     case BLUE_CHANNEL:    return gimage->comp_preview_valid[BLUE_PIX];    break;
  3919.     case GRAY_CHANNEL:    return gimage->comp_preview_valid[GRAY_PIX];    break;
  3920.     case INDEXED_CHANNEL: return gimage->comp_preview_valid[INDEXED_PIX]; break;
  3921.     default:              return TRUE;
  3922.     }
  3923. }
  3924.  
  3925. void
  3926. gimp_image_invalidate_preview (GimpImage *gimage)
  3927. {
  3928.   Layer *layer;
  3929.  
  3930.   g_return_if_fail (GIMP_IS_IMAGE (gimage));
  3931.  
  3932.   /*  Invalidate the floating sel if it exists  */
  3933.   if ((layer = gimp_image_floating_sel (gimage)))
  3934.     floating_sel_invalidate (layer);
  3935.  
  3936.   gimage->comp_preview_valid[0] = FALSE;
  3937.   gimage->comp_preview_valid[1] = FALSE;
  3938.   gimage->comp_preview_valid[2] = FALSE;
  3939. }
  3940.